6

Quick question. Using IB, I have a subview in a ViewController. In that subview I have a label, which I would like to wire to my custom subview class. However, IB will not let me. What am I missing?

I also tried to add the label programmatically; however, it appears that the frame was not ever set. I could hard code the size of the label, but I could not make it dependent on the frame size of my subview, because the frame and the bounds were always zero rects, even after the view showed up in my view controller at a non zero size. Any ideas here would also be much appreciated.

GLee
  • 5,003
  • 5
  • 35
  • 39
  • When you say "in that subview I have a label" how did it get there? Do you have a separate .xib file for the view (let's call it MyCustomView), and in MyCustomView.xib you have the label? Or do you have the label "inside" the subview in the OuterViewController.xib ? I'm asking because adding one of your own custom views to a view in IB doesn't usually show the custom view's subviews (eg your label). – Alan Rowarth May 18 '13 at 02:48

3 Answers3

13

You are actually completely right. It wont let you connect from IB to the Header of a custom view in Xcode 4.6.2

Personally I would file a Radar but I would want to do a bit more research to prove it and as this is a pattern I wouldn't ever use then I won't.

Fortunately you can get around it

Make sure your custom view is configured correctly in IB

enter image description here

and assuming you are setup something like this

enter image description here

Then you can manually declare in your header

@interface MyCustomView : UIView

@property (weak) IBOutlet UILabel *label;

@end

And drag FROM the dot that appears beside the property TO the label.

Or drag FROM the right-click HUD of the custom view TO the label.

Neither case will work by dragging from the label to the view.

Warren Burton
  • 17,451
  • 3
  • 53
  • 73
  • 1
    Ah awesome thanks. I feel like an idiot for not trying to go the other direction. Much appreciated. – GLee May 20 '13 at 21:52
  • I think I'm going to raise a bug as you used to be able to do this and I tried it last night with an OSX project and it didn't work there either. Something odd is afoot – Warren Burton May 21 '13 at 06:55
0

In your header file, you need to define the label as an IBOutlet, then you can drag from your file's owner to the label.

IBOutlet * lblSomeLabel;
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • You might want to give some more directions on a case where the file owner is a ViewController and not the custom view that is supposed to be connected to that outlet. – Till May 18 '13 at 02:44
-4

Disable AutoLayOut and try again.

Kyle Fang
  • 1,139
  • 6
  • 14