Can't Wire to Subview in IB

2019-06-19 02:55发布

问题:

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.

回答1:

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

and assuming you are setup something like this

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.



回答2:

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;


回答3:

Disable AutoLayOut and try again.