What is Referencing outlet collection in Xcode4 Interface Builder? What is Referencing outlet collection in Xcode4 Interface Builder? ios ios

What is Referencing outlet collection in Xcode4 Interface Builder?


The IBOutletCollection is a way to group IBOutlets. Imagine that you have 3 or 4 UILabels, on which you will apply a style (font, backgroundColour, opacity, etc). With a IBOutletCollection, it becomes trivial to do this. First you need to define your IBOutletCollection:

@property (nonatomic, retain) IBOutletCollection(UILabel) NSArray *labelsCollection;

(notice the type we are putting inside parenthesis, although we could put an id, if we had a mix collection)

Connect the IBoutlets on Interface Builder and then just iterate it:

for(UILabel *label in labelsCollection){    // Apply your styles}

Hope this helps you understand:

http://useyourloaf.com/blog/2011/3/28/interface-builder-outlet-collections.html


Ive just been hacking XIBs.

You can see a Outlet collection in use here:

A control can have multiple gesture recognizers which are stored in :

UITouch @property(nonatomic,readonly,copy) NSArray *gestureRecognizers

Open IB

Drag UITextView to a IB View.

Drag Pinch Gesture Recognizer to the textview.

Click on each in the tree of objects and open the Connections Inspector.

you'll see its been added a collection, not a single outlet.

OUTLET COLLECTIONSgestureRecognizers ------> Pinch Gesture


Using XCode Interface Builder create/connect your IBOutlets to the IBOutlet Collection. As result, you will get the following code in .h file:

@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *labels;

In the .m file you can iterate using for-loop to get your desired features like font size or color:

for (UILabel *label in self.labels) {        label.font = [UIFont systemFontOfSize:14];        label.textColor=[UIColor blueColor];}

or

@synthesize labels;...for (UILabel *label in labels) {        label.font = [UIFont systemFontOfSize:14];        label.textColor=[UIColor blueColor];}