Proud Member
Proud Member

Reputation: 40496

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?

Upvotes: 2

Views: 823

Answers (3)

TomSwift
TomSwift

Reputation: 39512

When an object is the "nib file owner" that means that it is already instantiated (not necessarily "init'ed") before the nib is loaded. When you load the nib it isn't creating the owner, it is only hooking up outlets to the owner.

I agree that it is confusing that you cannot set initial values for properties on a view controller initialized via initWithNibName using the Properties panel in IB.

However, I believe that you can set property values on the File Owner via the User Defined Runtime Attributes panel in IB on the file owner (XCode 4.2+, iOS5+ only).

Upvotes: 2

alexandresoli
alexandresoli

Reputation: 928

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;
}

Upvotes: 1

Adil Soomro
Adil Soomro

Reputation: 37729

You can only see/change some of the properties of Object which are in Object Section.

Typically the Objects which are in Object Section are set as IBoutlet to the files owner (UIViewController in your case), if you drag a UIViewController to Object section, you can edit it as well, but while talking about files owner, you can set its variable/properties/methods and only those variable/properties/methods which are either IBOutlets or IBAction.

If you want to set its title etc, you have to do that manually by code.

Upvotes: 1

Related Questions