Basic Navigation Controller via IB problems

Discussion in 'iOS Programming' started by iRandom, Jul 23, 2009.

  1. iRandom macrumors newbie

    Jul 23, 2009

    I've been trying to build a very basic navigation controller following the instructions given in:

    Interface Builder User Guide (IBUG)
    View Controller User Guide in iPhone OS (VCUG)

    The problem I have is that this doesn't seem to work. Specifically:

    After completing the steps given in the section "Configuring the Views for Additional Navigation Levels" in IBUG and getting something which looks exactly like Figure 3-10 in IBUG (except that the class of ViewController is set to my subclass, MyViewController), I attempt to load the child view using the code:

    MyViewController* myVC = [[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]] autorelease];
    [[self navigationController] pushViewController:myVC animated:YES];
    in the parent ViewController. However, an exception is throw which I've traced down to the fact that the outlet of the View object is not connected to anything (the ViewController view-outlet is connected to the View object however). Changing the File's Owner to MyViewController, and linking the View object's outlet to it solves the problem somewhat and the view loads, EXCEPT for the title. I've tried changing the Title attributes in ViewController and the NavigationItem, but on loading the view, the title is still blank. In the end, I've had to add the following code block into MyViewController.m to get it to work:

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self setTitle:@"MyNewTitle"];
    My questions are:
    1. Why does following the guide exactly result in the initial crash - is there something I've done wrong?
    2. How come setting the title ViewController in Interface Builder doesn't work like it said it should do in the User Guides and I had to resort to doing it programmatically?

    I realise that I've managed to get it to do what I want it do eventually, but I'd like to understand why the other methods didn't work and I had to hack around the "official" steps to getting something simple to work.

  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    OK, I just looked at that section of the IBUG. The way they say to load the nib is odd and not how it's normally done. You don't usually have a view controller inside the nib. I think the most likely reason to do that is if the nib is being loaded automatically from the main nib.

    To push a view controller onto the nav stack it's usually done like this:

    Create the .m/.h files for the view controller for this nib in Xcode.
    Create a nib from the template in Xcode.
    Set the class of the file's owner in the nib to match your new view controller.
    Add a view hierarchy to the nib.
    Make sure the view outlet of the file's owner is connected to the top level view in the view hierarchy.
    Connect all the other outlets and actions in the between views in the nib and the file's owner.

    Then use the three line code, like you show, to create the view controller and initialize it from the nib and then pushViewController.

    I've never set the title of the view controller in the nib. Always in code in viewDidLoad

    self.title = @"What a wonderful nib";
    I can't tell you why the IBUG is faulty.
  3. iRandom thread starter macrumors newbie

    Jul 23, 2009
    Thanks! I was wondering whether anyone else has experienced the same problems following the user guides step-by-step.

    I just presumed that adding a ViewController to the nib file was mainly to be able to set the title of the navbar via IB.
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Maybe. However if you create the view controller with initWithNibName then the view controller in the nib won't be the active view controller. In fact unless there's an outlet for it I think it will be autoreleased and just go away. I assume that the reason that you don't get the title to work that way is because the view controller in the nib isn't the active view controller so its title is irrelevant.

Share This Page