Brian Boyle
Brian Boyle

Reputation: 2879

iOS - Interface Builder Outlets Not Initialized

I have created a view in Interface Builder with some labels and text as IBOutlets. I can view this screen perfectly when I segue to it from another view that I have defined in my Storyboard. However, I have another XIB and an associated UIViewController that I want to access that view from. Because my XIB is not in the Storyboard, I cant segue to it. Instead I have to execute the transition programmatically.

    PlantDetailViewController *plantDetailVC = [[PlantDetailViewController alloc] init];
    [self.currentNavigationController pushViewController:plantDetailVC animated:YES];

When this code is executed it transitions to the view but the view is just blank. I have debugged the code and it enters viewDidLoad and viewWillAppear however all my IBOutlets are NIL....so nothing it showing up on screen!

Can anyone tell me why they might be NIL and how I can initialize them?

Thanks

Brian

Upvotes: 3

Views: 1841

Answers (2)

rob mayoff
rob mayoff

Reputation: 385600

It sounds like you're saying you have a PlantDetailViewController set up in your storyboard, and some OtherViewController that was created outside of your storyboard. Now you want OtherViewController to instantiate the PlantDetailViewController that was set up in your storyboard.

Let's say your storyboard is named MainStoryboard.storyboard.

First, you need to set the identifier of the PlantDetailViewController in your storyboard. You do this in the Attributes inspector, under the View Controller section. Let's say you set it to PlantDetail.

Then, in OtherViewController, this should work:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
PlantDetailViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"PlantDetail"];
[self.currentNavigationController pushViewController:vc animated:YES];

Upvotes: 2

Jonah
Jonah

Reputation: 17958

-init doesn't load a nib file for you, if you want to load a nib use -initWithNibName:bundle:

If you use nib naming conventions you can pass nil to load a nib whose name matches your class and the default bundle, i.e. [[PlantDetailViewController alloc] initWithNibName:nil bundle:nil], see the docs for -nibName for details.

Upvotes: 0

Related Questions