« Using Foundation Collections in Interface Builder
When you first played with Interface Builder, you may have tried briefly to create an array or a dictionary, and given up in disgust. Apple in its wisdom has not, as of Mac OS X 10.1.5, seen fit to add support for these fundamental classes to IB, and it is usually possible to work around this oversight.
// Foundation Collections Palette
To rectify this, you will find an IB palette at http://www.geocities.com/kritter_cocoadev called Foundation Collections Palette. You will need to compile the code with the supplied project, put the result somewhere and load it into IB. An extra palette should appear:
The left item here is a dictionary, the right an array, and from this palette you can drag collections to your nibs.
// Adding a Dictionary
Ever wanted to refer to objects in your nib by name? Older versions of Mac OS X/NextSTEP used to access everything in nibs by name, and we can replicate the functionality by adding objects to a dictionary. First, we drag in a dictionary to our nib; then we begin control-dragging connections from it to the various objects in our nib, naming them as we go.
Now we can connect our dictionary to the file owner and reference objects by name, objectEnumerator, or however we want.
[objects objectForKey:@"Video]To make this work in any project using the nib, we need to follow instructions and add nibObjectKeyConnector.h and nibObjectKeyConnector.m (in the NSDictionary folder of the downloaded code) to the project; otherwise, the application will die when it tries to load the nib.
Other ways you could use a dictionary is to store a list of numeric constants (NSNumbers can be stored in a dictionary using the attributes inspector) or localizable strings (NSStrings can also be added in the attributes inspector).
// Adding an Array
Ever wanted to iterate through a set of objects in a nib? Maybe you need to apply the same chunk of code to a lot of objects, such as a set of objects. While this can be done with copy-and-paste, it seems much neater to add an array to the nib and use its objectEnumerator to do the dirty work. As before, we drag an array to our nib and control-drag connections to the objects we want in it. (Note you need to confirm the connections by clicking Connect in the inspector.)
Once we have our filled array, we can rearrange the object to the order we want in the attributes inspector, deleting the default string the array starts with while we are there:
Finally, we can add the array to the file owner (or whichever object will be doing the iterating), and code its use in.
i = [buttons objectEnumerator]; while ((button = [i nextObject])) ...Once more following instructions, we add nibObjectIndexConnector.h and nibObjectIndexConnector.m to any project using the nib.
// Retain Cycles and Other Miscellany
Adding an object to a Foundation collection increases its retain count, and this is true of nibs just as anywhere else, so you must be careful to avoid retain cycles in your nibs. This boils down to not adding a collection that already, directly or not, contains it.
Dictionaries and arrays are equipped with a size inspector by FCP, which counts the objects stored in them; dictionaries also differentiate between connections to other objects and attributes (strings and numbers) in this count.
The code that makes FCP works is open source; feel free to plagiarize it freely. It shows how, for example, to enable multiple undo in your inspectors.
Foundation's collections are powerful; with Foundation Collections Palette they can be augmented with the power of Interface Builder.