What is Referencing outlet collection in Xcode4 Interface Builder?
Asked Answered
O

4

36

enter image description here

Here, I have pointed to the Referencing Outlet Collection. I am not able to figure out its usage in XCode4.

I am asking for the `new feature of REFERENCING OUTLET COLLECTION in InterfaceBuilder of XCode4".

Occidentalize answered 11/7, 2011 at 11:26 Comment(0)
P
59

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

Picofarad answered 11/7, 2011 at 11:47 Comment(0)
G
0

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 COLLECTIONS

gestureRecognizers ------> Pinch Gesture
Gooseneck answered 10/5, 2012 at 12:30 Comment(0)
E
0

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];
}
Eisenstein answered 11/11, 2014 at 0:10 Comment(0)
A
-1
swift: 

 // create outlet colllections
 @IBOutlet var name: [UILabel]!
    @IBOutlet var ageLabel: [UILabel]!
    @IBOutlet var genderLabel: [UILabel]!
    @IBOutlet var weightLabel: [UILabel]!
    @IBOutlet var heightLabel: [UILabel]!
    @IBOutlet var bmiLabel: [UILabel]!
    @IBOutlet var smokerLabel: [UILabel]!
    @IBOutlet var hdraLabel: [UILabel]!

// declare global vars
  var names: UILabel;
    var ageLabels: UILabel;
    var genderLabels: UILabel;
    var weightLabels: UILabel;
    var heightLabels: UILabel;
    var bmiLabels: UILabel;
    var smokerLabels: UILabel;
    var hdraLabels: UILabel;

// assign values
  for name:UILabel in self.name {
            self.names = name
        }

        for ageLabel:UILabel in self.ageLabel {
            self.ageLabels = ageLabel
        }

        for genderLabel:UILabel in self.genderLabel {
            self.genderLabels = genderLabel
        }

        for weightLabel:UILabel in self.weightLabel {
            self.weightLabels = weightLabel
        }

        for heightLabel:UILabel in self.heightLabel {
            self.heightLabels = heightLabel
        }

        for bmiLabel:UILabel in self.bmiLabel {
            self.bmiLabels = bmiLabel
        }

        for smokerLabel:UILabel in self.smokerLabel {
            self.smokerLabels = smokerLabel
        }

        for hdraLabel:UILabel in self.hdraLabel {
            self.hdraLabels = hdraLabel
        }
Afflatus answered 18/11, 2014 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.