0

Is the loading order of top level objects in a nib file random or can it be determined? I thought it would load the objects in the order I put them in in Interface Builder. But that doesn't seem to always be the case.

I wanted to create an inspector view similar to Interface Builder, where the different sections have alternating background colors. I planned to do this by ordering the sections as top level views in my nib and then color them according to the loading order.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • You should assign a tag to each of the views in Interface Builder. You can then use the tag to determine the color at runtime. – Rob Keniger Jun 04 '12 at 00:13

1 Answers1

5

According to the documentation, the loading order is undefined:

The order in which the nib-loading code calls the awakeFromNib methods of objects is not guaranteed. In OS X, Cocoa tries to call the awakeFromNib method of File’s Owner last but does not guarantee that behavior. If you need to configure the objects in your nib file further at load time, the most appropriate time to do so is after your nib-loading call returns. At that point, all of the objects are created, initialized, and ready for use.

See https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html

And regardless, relying on something like the loading order to determine the visual layout seems like a very non-robust idea. I would instead use an NSTableView / UITableView, which will do the layout, alternating row colors, etc. all for you. (On Mac, you probably want a view-based table view). If you need a custom solution for some reason, load and add the views dynamically in code where you have full control over their order and position in the parent.

Adam Leonard
  • 489
  • 2
  • 5