0

I am trying to load a nib file like this:

[[NSBundle mainBundle] loadNibNamed:@"PhotoViewController" owner:self options:nil];

but it makes the app crash

Mohammad Rabi
  • 1,412
  • 2
  • 21
  • 41
Alessandro
  • 4,000
  • 12
  • 63
  • 131

2 Answers2

1
[[NSBundle mainBundle] loadNibNamed:@"viewNib" owner:self options:nil];

You normally do this from the view controller you have set as File's Owner in the NIB. That way, you can declare an outlet for the view in the view controller which will automatically get connected when you load the NIB file.

Mohammad Rabi
  • 1,412
  • 2
  • 21
  • 41
1

If you're creating the view controller programmatically, you can use the initWithNibName:bundle: view controller method

I.e.

[[myViewcontroller alloc] initWithNibName:@"myNib" bundle:nil];

If you're transitioning to the view controller via storyboard, and want to have the storyboard load that view controller from a particular nib file, you could do the following:

  1. In the storyboard, delete the view controller's view
  2. Give the nib file the same name as the class of the storyboard view controller

This same question is also answered here: Using XCode storyboard to instantiate view controller that uses XIB for its design

Community
  • 1
  • 1
jal72088
  • 91
  • 3