50

I know this appears to be a duplicate of some other questions, but the answers are not working for me.

  1. I have created a single view app.
  2. In the storyboard I added a subview to my main view.
  3. I have a label on my main view and another label on my subview.
  4. I have created a class of type UIView and added it as the custom class for the subview.
  5. 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.
  6. 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.

Main view and subview in Interface Builder - custom class is set

The second image shows the main view label is connected in the main view's controller.

Main view and subview in Interface Builder - main view's label is connected

The third image shows that there are no outlet connections for the subview's label.

Main view and subview in Interface Builder - cannot connect subview's label

Patricia
  • 5,019
  • 14
  • 72
  • 152
  • 1
    Fascinating. It works with when you do it with custom cells in a table view but not when doing it with a custom view on a scene. – Rob Nov 29 '13 at 22:40
  • 1
    @Rob - Agree. I could swear it used to work. Maybe I'm just confused. – Patricia Nov 29 '13 at 22:42
  • @Rob - answer the question and I'll give you the points. You are correct. It should work. I found the answer and I'll post the details but nobody will get the points if I answer my own question. So answer it the way it should work and I'll fill in the blanks and give you the points. :-) – Patricia Nov 29 '13 at 23:16

5 Answers5

122

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.

enter image description here

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.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 2
    Yes. 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 Nov 30 '13 at 00:50
  • I actually found the answer here: http://stackoverflow.com/questions/16620130/cant-wire-to-subview-in-ib, so remember to up vote a question or answer if it helps!!! – Patricia Nov 30 '13 at 00:51
  • 1
    @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 Nov 30 '13 at 00:53
  • 1
    I 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. – Rob May 03 '15 at 21:25
  • You 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. – Josh Feb 12 '16 at 15:19
  • I just did that and it works perfectly fine for Swift as well! – fpg1503 Feb 18 '16 at 16:37
  • Wow, 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. – auspicious99 Apr 23 '18 at 09:24
4

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.

Choppin Broccoli
  • 3,048
  • 2
  • 21
  • 28
  • 2
    Lucy 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). – Rob Nov 29 '13 at 22:29
  • Is 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. – Patricia Nov 29 '13 at 22:35
  • 1
    @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). – Rob Nov 29 '13 at 22:41
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.

zevij
  • 2,416
  • 1
  • 23
  • 32
1

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;
Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81
  • OK. 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? – Patricia Nov 29 '13 at 22:45
  • @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. – Josue Espinosa Nov 29 '13 at 23:06
0

Zoom your storyboard to 100%. If you zoom out, to say 50%, then the outlet connection won't work.

Greg
  • 515
  • 1
  • 8
  • 19
Makku
  • 9
  • 1