How to make a connection to UIContainerView - ios

I have a standard extended UIViewController (called ParentViewController, or PVC), and want to have a container or child view which will be driven by ChildViewController or CVC.
I see that programmatically, that in PVC I can call addChild:CVC and manage calls such as didMoveToParentViewController, etc.... However, I am hoping that there is a way to do this with Interface Builder.
So I laid out an instance of PVC, then dragged a ContainerView onto PVC's view which creates a segue to a child window. In that window I assigned the class type of ChildViewController. This looks great so far, but how do I reference this programmatically from PVC? Let's try the usual: In PVC class extension, I have declared:
#property (nonatomic, strong) IBOutlet ChildViewController* cvc;
When I switch to IB, and highlight the PVC instance, and select the connections inspector, I see my cvc property but I cannot hook it up to my containerView instance (even though its class type is set appropriately). It allows me to drag (draw the line), but does not let me actually hook it up. Why??
I am thinking that this just isn't possible with IB, but perhaps I'm looking at this incorrectly?
I have read Apple's docs on child views and am prepared to do this programmatically if I can't get IB to do what I'm intending.

cvc is a child view controller of PVC, so you can get a reference to it with the childViewControllers property, which you would do in code rather than hooking up an outlet in IB. In PVC's viewDidLoad method, do this:
self.cvc = (ChildViewController *)self.childViewControllers[0];
Xcode will not allow you to hook up outlets between two different controllers, so even though cvc is embedded as a child view controller, it's still a different view controller.

Related

How do I connect an embedded ViewController to an IBOutlet of its parent?

I am using Storyboards to create a container for an embedded viewController.
I would like to connected it to an IBOutlet of its parent viewController. I have defined the property on the parent and tried to ctl-drag to the container and I don't get an option to connect.
The pop-up doesn't show the property.
Any thoughts?
If you're trying to make the outlet to the container view, you should be able to do that. It's just a normal UIView, so your IBOutlet property or ivar needs to be typed as UIView.
However, if you're trying to make the outlet between the parent controller and the embedded controller, you can't do that. IBOutlets can only be connected within a single controller, not across controllers. Instead of using an outlet, you can get a reference to that embedded controller in prepareForSegue -- that method will be called as soon as the parent controller is instantiated, and the child controller (the embedded one) will be the segue.destinationViewController.
In cording your said, You maybe forget to set Custom Class' asviewController` Class name.

Cannot create outlet connections to subviews in Interface Builder (Xcode 5)

I know this appears to be a duplicate of some other questions, but the answers are not working for me.
I have created a single view app.
In the storyboard I added a subview to my main view.
I have a label on my main view and another label on my subview.
I have created a class of type UIView and added it as the custom class for the subview.
I can ctrl-drag my label on my main view to the main view controller class. But when I try to ctrl-drag my label on my subview to my custom class, I cannot get the connection to occur.
I have even typed the property information and tried to make the connection manually to no avail.
Things have changed a bit in the latest version of Xcode's Interface Builder. Can somebody tell me what I am missing? There is literally no code here. I am just testing trying to connect outlets to a subview with a custom class.
The first image shows that I have set up the custom class and added a property but I cannot make the connection.
The second image shows the main view label is connected in the main view's controller.
The third image shows that there are no outlet connections for the subview's label.
You can manually write the IBOutlet property declaration in the #interface of the custom view subclass, and assuming you've defined the base class of your subview in IB, then you can drag from the outlet circle in the code back to the control in the scene.
Or, as you point out, Warren Burton suggested both this technique and another in his answer to this other question, Can't Wire to Subview in IB.
The issue has to do with the File Owner of the View Controller. It is probably set up as being IOViewController, thus you can only make property connections in that .h file.
What you can do, is create another .nib file for the subview and put the subview in there. Then in that .nib file, make the file owner IOSubview. Property connections will work just fine there. Then just add the subview to your IOViewController programatically. Just remember to load the nib file from bundle first.
This is what I did (in Swift):
I Created a new ViewController (e.g. class MyViewController: UIViewController {})
In StoryBoard, I expanded the 'Scenes' (i.e. the tree view of all UI components) and selected 'MyViewController'
Using the 'identity inspector' I assigned the 'MyViewController' class (as oppose to the default UIViewController)
After that I was able to assign an action.
I suspect that for Obj-C it is similar process.
You don't create outlets in the subclass, you create the outlet on the view controller it is on. You need to #import the subclass into IDViewController.h and create an outlet there.
IDViewController.h
#import "IDSubclass.h"
...
#property (strong, nonatomic) IBOutlet IDSubclass *outletName;
Zoom your storyboard to 100%. If you zoom out, to say 50%, then the outlet connection won't work.

How to load UIView from UIViewcontroller?

I have created a view and now I want to load that view from another viewcontroller without allocating the view. Is it possible or not. please clarify me.
I am doing like following
1)Added UIView in viewcontroller.xib and given view class name for the view.
2)created IBOutlet for the view and connected.
Now I want to access one property of a view without allocating. Is it possible to do or not.
If you've created an IBOutlet and hooked up the view, then you are done. The view is allocated when the xib loads and the reference is placed in the IBOutlet. You can access the view and it's properties through that without doing any allocating yourself.
When your view controller is loaded, your view connected to it (by IBOutlet) also gets loaded. In this case you have access to all view's property.
Look at "Looading up an XIB" section in this article.

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.

Unable to link UIViewController IBOutlets with StoryBoard

I have two UIViewControllers (A and B) in storyboard. The A view controller has a property
#property (nonatomic, retain) IBOutlet UIViewController *viewController;
that I want to link via storyboard to B.
The outlet shows under the the IBOutlets section in storyboard menu but I'm unable to link.
It might seem strange why I'm trying to do that but I need it. Someone knows how to do that?
IBOutlets are connections within a single view controller. Create references to objects inside the view controller so you can use those objects in code.
You cannot create IBOutlets from one view controller to another one. A property is the correct way to go, but you have to assign the property in code. Normally when one view controller creates another one, it might set a reference to itself.
OtherViewController *otherViewController = [OtherViewController alloc] init];
otherViewController.masterViewController = self;
// at this point "otherViewController" has a reference to the current view controller
Now I understand what I need to do. I need to create a custom segue to achieve the same result as when you link a UINavigationController to other ViewController and mark as RootViewController. It is done by segue and not by IBOutlet.
I am a bit late to the party however I put together a segue class to help with this job. View the git repository to see the class in action: https://github.com/datinc/DATOutletSegue.
Basically it uses the identifier of the segue to connect to the parent controller

Resources