开发者

Practical efficient usage of IBOutletColletion

How does look the practical usage of IBOutletCollection? Unfortunately Apple documentation mentions it briefly without giving an wider idea of usage. OK, it maintains开发者_Python百科 one-to-many relation with IB, but how to access and use particular objects efficiently? With TagName? How to ensure the order of objects?


I've recently used this to easily initialize a grid of labels. I have a n by n matrix of labels on a view, and reference each one individually (via an IBOutlet) in order to display relevant data. However when the view first loads I wanted to control the default text that displayed in all the labels. Initially i wanted a dash to display, but since this is for a client I wanted it to be easy to change. The view contents have and continue to change over time, per client requests.

Instead of writing N line of code, I created an IBOutletCollection and accomplished the same results in 4 (@property, @synthesize, and for loop). YMMV but I found it very useful in this situation.


Read again this section in the Interface Builder User Guide.

IBOutletCollections are actually just NSArrays you can connect to more than one object in IB. All the objects you connected end up in this array and can be accessed from code like any other object in an array.


I used it to minimize code. I have a range of UIViews that should react on "touch up inside" events of some UIButtons (custom mode).

I gave all UIButtons a tag (lets say 1005 to 1010) and all UIViews the same tag as the UIButton they shall respond to.

Then I connected the UIViews with the collection in Interface Builder. All UIButton touch up events go to the same function in my controller. This function gets the tag of the sender object, iterates through the NSArray list (of "IBOutletCollection(UIView)") and compares the tag. Everytime it hits, the appropriate action is done.

It is a pity that NSArrays seem not to hold the order...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜