开发者

how to access label in custom uiview built with ib

I created a custom view in IB for a picker row. The row has an imageview and label. Without creating a viewcontroller for the view, is there a way to access the subviews and mutate their properties?

Who would be the开发者_运维技巧 File Owner in the case where I have no view controller?

Thx!


Any object becomes the owner, and you can connect subviews to outlets of the file owner. So it depends on where you want to store references of the components. A typical pattern here is a subclass of UIView becomes the owner, which will represent the custom view.

If you do not want to use outlets or do not want to subclass UIView, then what you could do is to use tags. You can set a tag for each view object in IB. For example, set the tag of the label to be 1. Then, you can ask its superview for a subview object with a matching tag, using viewWithTag: method.


in IB, you can assign the label to have a specific tag. Then, you can call -viewWithTag: on the view to get the view in its view hierarchy with a specific tag.

UILabel *pickerLabel = [pickerView viewWithTag:42];


If you set tags for the subviews in interface builder, you can reference the views using the method on the parent view (of the subviews in question):

[parentView viewWithTag:3];

Where 3 is the number you set in IB for the subview's tag. You would put this in the line which manages or makes the view appear on screen or where ever you have a reference to the main/parent view of the subviews.


File's Owner can be any class. See Apple's tableview controller documentation for an example of how to load custom table-cells from Nib Files it walks through specifically how to configure this style scenario in IB. This pattern can be used generically not just with uitableviews.

http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html%23//apple_ref/doc/uid/TP40007451

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜