Search code examples
iphoneiosxcodexcode4interface-builder

Why can't I edit the properties of UIViewController in Interface Builder when it's the File's Owner?


I created a new "Tab Bar app" project that gave me a FirstViewController.xib and SecondViewController.xib.

In the AppDelegate a tab bar controller is configured like this:

UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;

This clearly loads an instance of UIViewController from a nib file, doesn't it? But when I open SecondViewController.xib the only root level object I see is the view of the view controller. This doesn't make sense to me, as I believe that the entire UIViewController instance is actually coming out of that nib.

Where is the rest? Why can't I see the UIViewController properties such as title? When I click Files Owner the Identity tab shows "SecondViewController", so Files Owner definitely is a UIViewController but it seems that it is not archived in the nib. The instance in fact seems to be created programmatically and the nib seems to archive only the view.

It is confusing because the name of the nib file is SecondViewController.xib. Can someone clarify?


Solution

  • Actually, the instance of SecondViewController is coming from SecondViewController.h. When you use initWithNibName:@"SecondViewController" it loads the nibs and its properties, but if you want to change any of those objets you need to connect them to SecondViewController using interface builder.

    Reproducing your problem i could get a SecondViewController.xib with the following views:

    View Label - Second View Text View

    If you look at SecondViewController.m you can see its changing the title in the method

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            self.title = NSLocalizedString(@"Second", @"Second");
            self.tabBarItem.image = [UIImage imageNamed:@"second"];
        }
        return self;
    }