2

I have a UIViewController named ViewControllerHome and when the user touches an image on the screen I would like to display a second view which is a Membership Card. I am doing the following from the touch of the image:

membershipCardViewController = [[MembershipCardViewController alloc] initWithNibName:@"MembershipCard" bundle:nil];
[self presentViewController:membershipCardViewController animated:YES completion:nil];

When the code executes an exception is thrown on the presentViewController line.

I have an .xib with a ViewController that contains a view and a UIImageView of the Membership Card. I set the class of the ViewController to my MembershipViewController.

Once that shows up I will dismiss it on a touch.

Can anyone tell me what I am missing? I thought I had all the steps correct to present the view controller.

Thanks for the help.

Wain
  • 118,658
  • 15
  • 128
  • 151
LilMoke
  • 3,176
  • 7
  • 48
  • 88
  • You're missing any details about the exception from your question... – Wain Oct 22 '13 at 20:32
  • *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "MembershipCard" nib but the view outlet was not set.' *** First throw call stack: – LilMoke Oct 22 '13 at 20:38
  • Did you connect the `view` outlet of the controller in the `XIB`? – Wain Oct 22 '13 at 20:40
  • Excuse me for my ignorance as I have not used xib's very much, mostly storyboards. What do you mean? I have a view outlet that is set t view. – LilMoke Oct 22 '13 at 20:42
  • i'll add a pic to my answer in a sec – Myron Slaw Oct 22 '13 at 20:47

2 Answers2

3

In the MembershipCardViewController's nib file, its view (what ever view it is controlling) is connected to the view controllers view outlet.

enter image description here

To do this control drag from files owner to the view you want to connect it to (the grey view in this case)

enter image description here

And you should get this:

enter image description here

Files owner should point to your MembershipCardViewController. Every view controller has a pointer to a view. I'm going to guess that you added some custom view after deleting the stock one. Control drag from files owner to that view to make the outlet. (If this outlet returns nil, an exception will be thrown).

To be safe, make sure file's owner (in the nib) is pointing to MembershipCardViewController (This probably isn't the problem but it sounds like you may have started with an empty nib).

enter image description here

To do this, click on files owner, and select the identity inspector on the right. Make sure the class says MembershipCardViewController

I answered another question before about this Am I right in saying initWithNibName:bundle is used to manually load nib files and that initWithCoder would be used as an alternative?

This explains what is actually going on.

Community
  • 1
  • 1
Myron Slaw
  • 886
  • 9
  • 21
  • Yes,I did star5t with an empty nib, but my MembershipCardViewController is not in the custom class drop down. Am I not supposed to set that also? – LilMoke Oct 22 '13 at 20:48
  • Yes, I understand, but when I Click on File's Owner, I do not have an Outlets Section, just Referencing Outlets and Referencing Outlet Connections. – LilMoke Oct 22 '13 at 20:57
  • control drag from files owner to the view you want to connect it to. The view outlet should show up – Myron Slaw Oct 22 '13 at 21:02
  • Ok, for some reason the class was not in the dropdown, but I did type it in and it recognized it, but now I get: reason: 'A view can only be associated with at most one view controller at a time! View > is associated with . Clear this association before associating this view with . – LilMoke Oct 22 '13 at 21:02
  • OMG, rookie mistake... I had a ViewController in the nib as well, I removed that and everything works as expected. Thanks you for your help!! – LilMoke Oct 22 '13 at 21:04
  • I've made it a million times :) – Myron Slaw Oct 22 '13 at 21:07
  • Thank you for your help!!! Sometimes you just need someone to talk to about your foolish mistakes and you then see the light!! Thanks for the help!! – LilMoke Oct 22 '13 at 21:09
1

The purpose of your XIB is to archive the view of the controller. Having the controller class set is only part of the required information, you also need to connect any IBOutlet relationships between the controller and the views.

As standard any subclass of UIViewController provides an outlet called view. You need to ensure that it's connected. Otherwise when you load the XIB the view doesn't get set and you get an exception.

There are a number of ways to make the connection. Check this.

See also loaded-nib-but-the-view-outlet-was-not-set-new-to-interfacebuilder.

Community
  • 1
  • 1
Wain
  • 118,658
  • 15
  • 128
  • 151