views:

70

answers:

2

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

A: 

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.

Sven
That's exactly what is described in Apple's documentation :) Now, how looks the practical usage? You said, I can access every object as any other object in any other array. True, but how this array is sorted? Should I sort it by my own at the beginning? And how to distinguish objects in this array? With tags? If so, should I iterate through the entire array to find the objects I'm looking for, or it's better to search the array with predicate? An so on...
juckobee
If you’re looking for specific objects you probably shouldn’t be using this. Use it if you want to be able to iterate over a list of elements. If you need to distinguish them you could use the `tag` property, or maybe the `target` and/or `action`.
Sven
+2  A: 

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.

logancautrell