Help with multiple views

Discussion in 'iOS Programming' started by teengohan, May 22, 2011.

  1. teengohan, May 22, 2011
    Last edited: May 22, 2011

    macrumors newbie

    Joined:
    May 20, 2011
    #1
    Hi all,
    I am having a lot of trouble figuring out how to have multiple views in my app. I need to have 3 and every tutorial I have looked at is written for xcode 3. Does anyone know where I can get a walkthrough of this for xcode 4? I have tried doing it based on those tutorials and the text book I bought but I always get a load of errors that they don't. I learn by doing, if I can see it done once I'll be able to figure it out and understand whats going on.

    Another wasy would be to see an example of an app with only 2 or 3 views as every example I've found has either 1 or loads.

    I'd really appreciate any help with this.

    EDIT:
    From looking at xcode it looks like it is possible to have multiple views under one xib file, if this is the case where are the viewcontrollers for these views?
     
  2. macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #2
    Which book are you using?
     
  3. thread starter macrumors newbie

    Joined:
    May 20, 2011
    #3
    I'm using Iphone application development for dummies. It's a great book but it hasn't helped me figure out how to do multiple views as the author is not writing the xcode 4 version of the book till this summer.
     
  4. macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #4
    I have that book. Let me look at it and I'll get back to you. Which chapter are you using for multiple views?
     
  5. teengohan, May 22, 2011
    Last edited by a moderator: May 23, 2011

    thread starter macrumors newbie

    Joined:
    May 20, 2011
    #5
    On page 284 he discuses navigating between views in an industrial strength application. Later on at page 344 he starts of making his other views.

    Just so you know what I'm doing, I need a first view with a button that opens a second view with a table, each cell in the table links to a view that displays an image.

    when I create a new view controller it starts with several errors before I even start coding. First of all in the new controlers .h file I get an error saying that this class is trying to recursively use itself as its own superclass.

    Code:
    @interface SecondViewController : SecondViewController {
    
    I fix this by changing it to a generic UIViewController

    Code:
    @interface SecondViewController : UIViewController {
    
    Not sure if this is right but it's all I could think to do. So at this point I have my second view. I put in a label so I can see when I manage to navigate to it properly and I try to follow the instructions on page 348 to set the views owner as the first view but interface builder won't let me do it.

    I am not following the instructions in the book exactly as I am only trying to make two linked views at this point so I can learn how it's done. I left out all of the code creating the table in the previous chapter.

    Edit: sorry about all the typos, still getting used to my first apple keyboard :S

    One thing that is definitely confusing me is when I create a new view based appplication a view controller is automatically generated. In the examples I have seen this seems to be something only xcode 4 does.

    The tutorials all tell me to make a rootViewController, can I use the auto generated viewcontroller for this or do I need to delete it so I can make this file?
     
  6. macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #6
    In the book, the author starts with a Navigation-Based Application, but you said you started with a View-Based Application. There is a difference between these two templates. Also, a View-Based Application always creates a view controller. so, do you have a navigation controller in your app? If you do, it's much easier to add and remove views.

    As for your tableview, there is a method called didSelectRow that gets called whenever a row of the table is selected. This is where you add code to have the new view added to the stack.
     
  7. thread starter macrumors newbie

    Joined:
    May 20, 2011
    #7
    In the time since my last post I have started a new Navigation-Based Application. The porblem is that the default rootviewcontroller created here is a table view and I need it to be a normal UIview as the teble view has to be the second in my hiarchy. I have tried changeing it to a UI view but I end up with errors I'm not sure how to fix yet when I do.

    Do you know how to create a navigation based app that does not default with a table view? Also would it be possible to post some sample code that will move me to another view?
     
  8. Shawnpk, May 22, 2011
    Last edited by a moderator: May 23, 2011

    macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #8
    I'm not near my computer at the moment, but if you change the is line:

    Code:
    @ interface ViewOneController: TableViewController
    to this:

    Code:
    @interface ViewOneController: UiViewController
    then, goto Interface Builder and delete the table view, that should do it. There's a little more to it, but like I said I'm not currently in front of my Mac. As soon as I get home, I'll look into.
     
  9. thread starter macrumors newbie

    Joined:
    May 20, 2011
    #9
    Thanks for the time you are spending with me, I appreciate it. For when you gett home I tried that and got a NSInternalInconsistencyException at run time. It says the view outlet was not set. I'll try and work this out formyself before you get home...
     
  10. teengohan, May 22, 2011
    Last edited: May 22, 2011

    thread starter macrumors newbie

    Joined:
    May 20, 2011
    #10
    So I made a navigation based app, made the changes you told me to, then realised I had to change the RootViewController.xib file by creating a new window and linking it to the view outlet. So now the app loads and I see my new view :)

    Next I created a new UIViewController subclass called TableViewController, changed its interface from UIViewController to UITableViewController and copied the meathods for a table view from the rootviewconrtoller into the new TableView.m file and so far so good. Now I am going to write the code to populate my table.
     
  11. Shawnpk, May 22, 2011
    Last edited: May 22, 2011

    macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #11
    By "on the default UIViewController methods" do you mean on the original rootViewController, or on the new UITableViewController?
     
  12. thread starter macrumors newbie

    Joined:
    May 20, 2011
    #12
    It was in the TableViewController.m file as I had acidently changed it to UITableView instaid of UITableViewController, I fixed that and the warnings disapeared so I edited that out of the post since I had just made a silly mistake.
     
  13. macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #13
    I'm finally in front of my computer. Let me know if/how I can help.
     
  14. thread starter macrumors newbie

    Joined:
    May 20, 2011
    #14
    Well now i have managed to create all of my views properly. I started a new navigation based app and figured out how to make the app load a different view from rootviewcontroller first. So now my app loads into a blank page with a button on it. Now I am still trying to figure out how to navigate from this view to my rootviewcontroller when I press the button on the initial view. Are you able to give me any advice figuing this out? I am slowly getting it myself but it is giving me a major headache :S
     
  15. macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #15
    What type of view are you trying to push?
     
  16. thread starter macrumors newbie

    Joined:
    May 20, 2011
    #16
    When you say push do you mean what view am I trying to make appear? My app opens into a UIViewController, when the button on this view is pressed the rootViewController appears which is a UITableViewController.

    I actually found sone code in a project a friend send me.

    Code:
    - (IBAction) goToRootView: (id) sender
    {
        [self.navigationController popViewControllerAnimated:NO];
        
    }
    
    I attached the "touch up inside" event on my button to this meathod and I can now navigate to my table.

    My problem now is I don't really understand what this code did. Is this meathod always going to send me to the root view controller from any view?

    From what I can tell my table is set as a navigation controller which I believe is responcible for managing a view hiarchy so this code will be no use for navigating back to my first page.

    Am I getting close to understanding this?
     
  17. Shawnpk, May 22, 2011
    Last edited by a moderator: May 23, 2011

    macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #17
    When using a NavigationController, you can push and pop views. Pushing a view sets the view on the top of the stack. Poping a view removes the top view from the stack. It's a first on, first off mechanism. What you are doing is taking the UIViewController and removing it from the stack when you say popViewController animated. You can also push a view onto the stack with

    Code:
    [self.navigationController pushViewController: self.NameOfViewControllerInstance animated: YES];
    If you push a view onto the stack, you should get a button created for you on your navigation bar. This button will automagically pop the view you pushed onto the stack and return you to your original view.
     
  18. thread starter macrumors newbie

    Joined:
    May 20, 2011
    #18
    Right, so when my app starts the root view controller is at the bottom of the stack with my front page view on top of it, the code I posted before popped it off so the table became visable. Now all I need to do to return to my first view is push the firstview back onto the stack. Also I want to go to a 3rd view when a selection is made on my table, to do this I need to push the correct view onto the stack based on the cell on the table which is selected.


    Have I understood this properly? I feel like it's starting to make sense to me. Assuming I got all that right when I push the original view back onto the stack (when I want to return to it) is there any way to hide the automagically generated back button so the page looks like it did when the app loads?
     
  19. macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #19
    This will be much easier if you post your code. Please post the .h and .m files for the rootViewController and your secondviewcontroller. Thanks.
     
  20. thread starter macrumors newbie

    Joined:
    May 20, 2011
    #20
    Hi
    here are the files:

    RootViewController.h

    Code:
    
    #import <UIKit/UIKit.h>
    
    @interface RootViewController : UITableViewController {
    }
    
    
    @end
    
    RootViewController.m

    Code:
    
    #import "RootViewController.h"
    
    @implementation RootViewController
    
    - (void)viewDidLoad
    {
    	[super viewDidLoad];
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
    	[super viewWillDisappear:animated];
    }
    
    - (void)viewDidDisappear:(BOOL)animated
    {
    	[super viewDidDisappear:animated];
    }
    
    /*
     // Override to allow orientations other than the default portrait orientation.
     - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     // Return YES for supported orientations.
     return (interfaceOrientation == UIInterfaceOrientationPortrait);
     }
     */
    
    // Customize the number of sections in the table view.
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    	return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    	return [listOfProductNames count];
    }
    
    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    	
        return 0;
    }
    
    /*
     // Override to support conditional editing of the table view.
     - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
     {
     // Return NO if you do not want the specified item to be editable.
     return YES;
     }
     */
    
    /*
     // Override to support editing the table view.
     - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
     {
     if (editingStyle == UITableViewCellEditingStyleDelete)
     {
     // Delete the row from the data source.
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
     }
     else if (editingStyle == UITableViewCellEditingStyleInsert)
     {
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
     }   
     }
     */
    
    /*
     // Override to support rearranging the table view.
     - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
     {
     }
     */
    
    /*
     // Override to support conditional rearranging of the table view.
     - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
     {
     // Return NO if you do not want the item to be re-orderable.
     return YES;
     }
     */
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        /*
    	 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    	 // ...
    	 // Pass the selected object to the new view controller.
    	 [self.navigationController pushViewController:detailViewController animated:YES];
    	 [detailViewController release];
    	 */
    }
    
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Relinquish ownership any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
    	
        // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
        // For example: self.myOutlet = nil;
    }
    
    - (void)dealloc
    {
        [super dealloc];
    }
    
    @end
    
    And this is my InitialViewController, this is the view that the app opens with:

    InitialViewController.h
    Code:
    #import <UIKit/UIKit.h>
    
    
    @interface InitialViewController : UIViewController {
        
    }
    
    - (IBAction) enterButton: (id) sender;
    
    @end
    InitialViewController.m

    Code:
    #import "InitialViewController.h"
    
    
    @implementation InitialViewController
    
    - (IBAction) enterButton: (id) sender
    {
        [self.navigationController popViewControllerAnimated:NO];
        
    }
    
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)dealloc
    {
        [super dealloc];
    }
    
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    @end
    
    The only thing I have removed from this is the code that populates the table as that uses information from a company that I don't think I should be sharing.

    I also realised that the code I was using to load the InitialView first is actually a push command like you described, I had copied and pasted it from another website without realising what I was doing.

    I actually think I'm ok now, your explination of the stack really helped me. so unless I got something horribly wrong in my last post I should be able to finnish this on my own.

    Thanks again for your help it has made a big difference :D
     
  21. macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #21
    Here is the code I wrote. Maybe if you get stuck it will help.

    RootViewController.h:
    Code:
    #import <UIKit/UIKit.h>
    #import "TableView.h"
    @interface RootViewController : UIViewController 
    
    {
        TableView *secondView;
        UIButton *button;
    }
    
    @property (nonatomic, retain) TableView *secondView;
    @property (nonatomic, retain) IBOutlet UIButton *button;
    
    - (IBAction) pushSecondView: (id) sender;
    
    @end
    RootViewController.m
    Code:
    #import "RootViewController.h"
    
    @implementation RootViewController
    
    @synthesize secondView, button;
    
    - (IBAction) pushSecondView:(id)sender
    
    {
        TableView *view = [[TableView alloc] 
                                     initWithNibName: @"TableView" 
                                     bundle: nil];
        
        self.secondView = view;
        [view release];
        
        [self.navigationController pushViewController: secondView animated: YES];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        self.title = @"Test";
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
    	[super viewWillDisappear:animated];
    }
    
    - (void)viewDidDisappear:(BOOL)animated
    {
    	[super viewDidDisappear:animated];
    }
    
    /*
     // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    	// Return YES for supported orientations.
    	return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
     */
    
    // Customize the number of sections in the table view.
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 1;
    }
    
    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        
        cell.textLabel.text = @"Test";
    
        // Configure the cell.
        return cell;
    }
    
    /*
    // Override to support conditional editing of the table view.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Return NO if you do not want the specified item to be editable.
        return YES;
    }
    */
    
    /*
    // Override to support editing the table view.
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (editingStyle == UITableViewCellEditingStyleDelete)
        {
            // Delete the row from the data source.
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
        else if (editingStyle == UITableViewCellEditingStyleInsert)
        {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
        }   
    }
    */
    
    /*
    // Override to support rearranging the table view.
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
    {
    }
    */
    
    /*
    // Override to support conditional rearranging of the table view.
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Return NO if you do not want the item to be re-orderable.
        return YES;
    }
    */
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        /*
        <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
        // ...
        // Pass the selected object to the new view controller.
        [self.navigationController pushViewController:detailViewController animated:YES];
        [detailViewController release];
    	*/
    }
    
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Relinquish ownership any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
    
        // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
        // For example: self.myOutlet = nil;
    }
    
    - (void)dealloc
    {
        [super dealloc];
    }
    
    @end
    TableView.h
    Code:
    #import <UIKit/UIKit.h>
    #import "ThirdViewController.h"
    
    @interface TableView : UITableViewController 
    
    {
        ThirdViewController *thirdView;
    }
    
    @property (nonatomic, retain) ThirdViewController *thirdView;
    
    @end
    TableView.m
    Code:
    #import "TableView.h"
    
    @implementation TableView
    
    @synthesize thirdView;
    
    - (id)initWithStyle:(UITableViewStyle)style
    {
        self = [super initWithStyle:style];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)dealloc
    {
        [super dealloc];
    }
    
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        self.title = @"Table View";
    
        // Uncomment the following line to preserve selection between presentations.
        // self.clearsSelectionOnViewWillAppear = NO;
     
        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    }
    
    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    //#warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    //#warning Incomplete method implementation.
        // Return the number of rows in the section.
        return 1;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        
        // Configure the cell...
        
        cell.textLabel.text = @"Test";
        
        return cell;
    }
    
    /*
    // Override to support conditional editing of the table view.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Return NO if you do not want the specified item to be editable.
        return YES;
    }
    */
    
    /*
    // Override to support editing the table view.
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // Delete the row from the data source
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }   
        else if (editingStyle == UITableViewCellEditingStyleInsert) {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }   
    }
    */
    
    /*
    // Override to support rearranging the table view.
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
    {
    }
    */
    
    /*
    // Override to support conditional rearranging of the table view.
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Return NO if you do not want the item to be re-orderable.
        return YES;
    }
    */
    
    #pragma mark - Table view delegate
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Navigation logic may go here. Create and push another view controller.
        /*
         <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
         // ...
         // Pass the selected object to the new view controller.
         [self.navigationController pushViewController:detailViewController animated:YES];
         [detailViewController release];
         */
        
        ThirdViewController *view = [[ThirdViewController alloc] 
                                     initWithNibName: @"ThirdViewController" 
                                     bundle: nil];
        
        self.thirdView = view;
        [view release];
        
        [self.navigationController pushViewController: thirdView animated: YES];
    }
    
    @end
    ThirdViewController.h
    Code:
    #import <UIKit/UIKit.h>
    
    
    @interface ThirdViewController : UIViewController {
        
    }
    
    @end
    ThirdViewController.m
    Code:
    #import "ThirdViewController.h"
    
    
    @implementation ThirdViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)dealloc
    {
        [super dealloc];
    }
    
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
        
        self.title = @"Third View";
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    @end
    The NIB files are pretty basic. Just labels and buttons to push the views. The button in the Navigation Bar is what pops the views off of the stack. You can see that particular button tells you which view you are going back to.

    **Sorry for the long post
     

    Attached Files:

  22. thread starter macrumors newbie

    Joined:
    May 20, 2011
    #22
    You've done more than enough. Before you posted that last page I managed to get my navigation code working perfectly. All I need to do now is display the correct information when a user selects a cell on the table which I know how to do. Your explination of the stack really helped me out :D

    perhaps I can sleep now :S

    Thanks so much
     

Share This Page