Cannot create outlet connections to subviews in Interface Builder (Xcode 5)
Solution 1
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.
Solution 2
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.
Solution 3
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.
Solution 4
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;
![Patricia](https://i.stack.imgur.com/ee6KW.png?s=256&g=1)
Patricia
News addict & avid reader. Software Engineer & Architect; strong emphasis in OO, ORM and database design using SysML, UML, and ER diagrams. I like knowledge and money, because on earth both are power. I like honesty and kindness, because in my heart both are sincere. I like humility and compassion, because in Heaven both are divine. May we all be filled with the love of God and be blessed by the existence we cannot see! Tenacious P, named by my coworkers, because I NEVER quit.
Updated on March 05, 2020Comments
-
Patricia over 4 years
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.
-
Rob over 10 yearsLucy is using storyboards (which don't use the File Owner concept). If using NIBs, this would be the right thing to check first, but this is not applicable to storyboards. And Lucy has set the base class for the view controller (which is the storyboard equivalent to setting the NIB's File Owner).
-
Patricia over 10 yearsIs there a way to change the File Owner? I believe there used to be but, as I said, there are changes to the newest version of Xcode. I want to use the redraw method on the subview to properly layout the contents.
-
Rob over 10 years@Lucy The "file owner" concept doesn't exist with a storyboard. Only with NIBs. The analog of "file owner" is the base class for the view controller (which you've set).
-
Patricia over 10 yearsOK. I did that. I want to use the redraw method on the subview to properly layout the contents. So-o-o-o-o-o-o, how can I connect the controls in the subview to the custom UIView class?
-
Josue Espinosa over 10 years@Lucy: I've never added outlets to a subclass, but what you can do, is call the drawRect method of UIView and dynamically draw any labels you want.
-
Patricia over 10 yearsYes. But it has changed in the latest version. Still very similar but instead you would follow these steps: Open the main view (which contains the subview) either in the storyboard or in the xib. Select the subview. Show the connections inspector. As long as the outlets have been added manually they will be included in the connections inspector. Control-click on the circle in the connections inspector and drag to the control on the subview. The control will be highlighted and the connection will be made when you release. That works!!! :-)
-
Patricia over 10 yearsI actually found the answer here: stackoverflow.com/questions/16620130/cant-wire-to-subview-in-ib, so remember to up vote a question or answer if it helps!!!
-
Rob over 10 years@Lucy Excellent. FYI, the above is in the latest Xcode, too. Sounds like there are a couple of work-arounds, but sadly, the most automated solution doesn't work!
-
Rob about 9 yearsI just tested it in Xcode 6.3.1 and it works fine. But more importantly, the bug that prevents you from just control dragging directly from the
UILabel
in a container view to the@interface
is remedied, too, eliminating the need to "go backwards". If its not working for you, double check that the base class for the scene is set to reference the view controller. -
Josh over 8 yearsYou sir are a saint. I just spent 30 minutes trying to figure this out in swift. It only works going from the outlet in the code to the storyboard. I'm in Xcode v7.2.1.
-
fpg1503 over 8 yearsI just did that and it works perfectly fine for Swift as well!
-
auspicious99 about 6 yearsWow, this "feature" is still present in Xcode 9.2, where I could not drag from the subview (a button in this case) to the code, as I normally do, but dragging from the outlet circle at the side of the code, back to the button, worked.