Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 542
Brian Boyle Avatar asked Feb 13 '26 04:02

Brian Boyle


1 Answers

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];
like image 161
rob mayoff Avatar answered Feb 15 '26 17:02

rob mayoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!