3

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

Brian Boyle
  • 2,849
  • 5
  • 27
  • 35

2 Answers2

2

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];
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
0

-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.

Jonah
  • 17,918
  • 1
  • 43
  • 70
  • Thanks for the response Jonah. However, I dont have a NIB for the view that I'm trying to load. The view is defined in a Storyboard and from what I understand there are no NIB files in Storyboard. Corect me if I'm wrong though. – Brian Boyle Mar 30 '12 at 01:08
  • 2
    Calling `UIViewController`'s `init` method is the same as calling `initWithNibName:nil bundle:nil`. If there is a nib with the same name as the view controller's class (or the same name without the word `Controller` on the end), the view controller will load that nib in `loadView`. – rob mayoff Mar 30 '12 at 03:11