Present UIViewController from another UIViewController - ios

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.

In the MembershipCardViewController's nib file, its view (what ever view it is controlling) is connected to the view controllers view outlet.
To do this control drag from files owner to the view you want to connect it to (the grey view in this case)
And you should get this:
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).
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.

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.

Related

Accessing a ViewController's subview that's added on the storyboard

Please see the image below for a storyboard visual. I have a ViewController called StudioViewController (It's labeled ViewController on the storyboard). And I have a UIScrollView that's called CanvasViewController (the thing to the right of the view controller that actually sits on top of the View Controller). I want to be able to access the CanvasViewController in the StudioViewController class, how do I do that? Because I created it in the storyboard here, I dont' seem to have a variable that allows me to access the CanvasViewController... Should I create it in code instead, or is there some obvious way to access it that I'm completely missing?
Thanks
******** UPDATE *********
The question has been answered in the comments below by rdelmar:
You could make a shared variable in the app delegate. This is not very secure but I have resulted to this when I can't access a child or parent due to page transitions.

Can't set view outlet in xib

I have a xib file that includes a view controller but I have the same problem as this link: problem
In this answer they can easily set outlet because view has a circle that is clickable.But in my case the view outlet is not even clickable.So I can't set the outlet.What to do now?
The correct way to use initWithNibName:: is to have a "View" IB document where you have the desired VC view outlet as a root element. You need to set the "File's Owner" "Class" to your UIViewController subclass and connect it's view outlet:
Also, don't present modal VC from self at viewDidLoad: at the time of this method execution the VC is often not yet presented itself, viewDidAppear: is more fitting for such tests.
I had this same problem, but neither this nor any other solutions seemed to work - what I wound up doing was setting the custom class to UIViewController, linking the view as described in the question link, and then changing the class back to the actual custom class name I had intended. The link stayed and everything worked from that point on.

Copy and paste storyboard controller into xib file. Supported or not?

I want to refactor out a storyboard controller to a separate xib, for better reusablity.
The controller extends UITableViewController.
I can select the controller in the storyboard, hit Cmd-C, create an empty xib file using the New File wizard, and finally paste the controller into the xib.
It seems to work ok. The UI elements are there, the outlets are there, etc. It forgets the orientation and the size (portrait, Retina 4-inch), but I can easily set that in the properties pane to the right in xcode.
However when trying to use this xib I get a crash with error message:
loaded the "MyController" nib but the view outlet was not set.
When I try to connect Files Owner to the table view (which is the top view in a controller extended from UITableViewController), xcode won't let me connect them.
My question: Is it unsupported to copy-paste a controller from a storyboard to a xib?
You are most likely jamming a tableViewController into a nib and there is no outlet to hook the view to. When you do Ctl-C in the storyboard make sure you copy the UITable not the UITableViewController. If not you can try to manually create the outlet in the controller but you might have to jump through hoops to make it work.

In IB, Cannot Connect IBOutlet Delegate to Another View Controller?

I have a view controller which as one of its views a container view (in IB storyboard) which embeds a table view controller, which in turn has a container view that embeds yet another view controller. In this last view controller I set up a delegate protocol with a weak synthesized delegate property as an IBOutlet. The very first view controller is what I want to receive the delegate methods from the last and I added the protocol <...> to it.
The problem is that I have not figured out a way while in storyboard (or otherwise) to link the IBOutlet delegate of the last view controller to the first view controller which follows the protocol so the last can send the first messages. I thought I could just drag and drop (with the control key) but all I find is segue options on the destination. It seems even though the delegate appears in the outlet connections window, it will not connect to ANY view controller in my project.
Can't ANY view controller be a delegate of another's protocol? And can be linked in IB? If I cannot do it with IB, I don't know how to make another VC a delegate upstream.
Any advice would be appreciated. Thanks.
You cannot connect IBOutlets between view controllers. You need to do it in code. You'll have to go through the chain of childViewController to get from the first controller to the last -- if I understand your structure properly, from the first view controller:
LastController *last = ((UIViewController *)self.childViewControllers[0]).childViewControllers[0];
last.delegate = self;
I asked a similar question Interface Builder won't allow connections to custom UIView class? I ultimately opened a bug with Apple. After a few follow up queries from Apple, I haven't heard any kind of resolution.

File's Owner of UITabBarController Subviews

I followed this UITabBarController Tutorial which creates a Tab Bar with according subviews mostly using Interface Builder. The UITabBarController is created there and the Tab's View Controllers are added there too.
Am I correct that creating the UIViewControllers {WelcomeViewController|AboutViewController}.{h|m} is unnecessary?
Who is the real File's Owner of the Subviews {WelcomeViewController|AboutViewController}.xib?
Note that I at first tried to create an IBAction method in WelcomeViewController.h: in Interface Builder at WelcomeViewController.xib, I could connect a button press to that action as it appeared at File's Owner. But at runtime it crashed, as the real File's Owner presumably is not an Object of WelcomeViewController.m. Am I right here? Is it a bug that the IBAction appears in Interface Builder (Xcode 4 here)?
A last question: How/can I still separate code (having IBActions in WelcomeViewController.h for actions that happen only on this subview) when I connect everything up in Interface Builder like in the tutorial?
Am I correct that creating the UIViewControllers {WelcomeViewController|AboutViewController}.{h|m} is unnecessary?
No, both controllers are necessary, since there should be at least (and, optimally, at most) one ViewController per full-screen window to manage your view hierarchy. The TabBarController is only a kind of "dumb" meta-controller managing the display of the sub-controllers it loads - therefore you need controllers for the views which are switched. I would recommend you read this part of the Apple doc.
Who is the real File's Owner of the Subviews {WelcomeViewController|AboutViewController}.xib?
The File's Owner should be the corresponding controller class (in your case, {WelcomeViewController|AboutViewController}.{h|m}) - you can set the class in Interface Builder in the inspector palette when File's Owner is selected. Only the very first window (usually called Main.xib or so) which is opened at application start should have the application delegate as File's Owner. File's Owners own the objects of the XIB/NIB file - object-reference wise, you know what I mean :) I think it should also be possible to load the XIB/NIB file with other controllers (and the other controller automatically becoming the File's Owner), but I'm not sure.
... Am I right here? Is it a bug that the IBAction appears in Interface Builder (Xcode 4 here)?
It may be that you wired up the action the wrong way in Interface Builder, a common mistake. Try holding the Ctrl key, then drag a line from the button onto the File's Owner, and choose the desired method to link to. That should do it.
A last question: How/can I still separate code (having IBActions in WelcomeViewController.h for actions that happen only on this subview) when I connect everything up in Interface Builder like in the tutorial?
I think I answered this in the first paragraph - WelcomeViewController is still there and all you have to do is create IBOutlets and wire them up in IB. Of course, you can also do the wiring programmatically, since the member "view" is automatically populated (via the File's Owner connection), and all subviews are accessible from there.

Resources