views:

536

answers:

1

I might not be using IBOutlet correctly, or some other subtlety with how NIB files work is causing me trouble - any help would be much appreciated (feel free to propose an alternate way to accomplish what I want).

I have a View object and a Controller object. Both are in the NIB. The Controller's init is also called when the NIB is loaded and the View is initialized in the 'awakeFromNib' callback. I need a way to connect these two objects - specifically, enable the 'View' object to call functions on the Controller.

Based on documentation online, the way to get these connected is to define an IBOutlet in the View and connect it to the Controller in the Interface Builder. So i created an

IBOutlet Controller* _controller;

in the View interface and graphically connected it to the Controller object in Interface Builder by making a connection from the View to the Controller and assigning the _controller outlet to the Controller (the blue Generic Object box in Interface Builder).

At runtime though, _controller is always _nil. I have verified that the Controller's init was indeed called.

Is there something obvious I'm missing about this? Any simpler way to connect these two? Since they're both created by the NIB I don't have a common object that has a pointer to both.

A: 

Try accessing the IBOutlet in viewDidLoad instad.

When awakeFromNib is called not all the IBOutlets are populated (even though the documentation seem to imply it).

pgb
I'm not trying use it in awakeFromNib... I am using it much later, when a menu click results in some code executing in the View object.Is there some refcounting foo I need to worry about?
Btw, I expect that once the IBOutlet is set it should be available until cleanup is done....Is that valid?
All IBOutlets *are* set by the time of awakeFromNib, unless you hadn't set them in the nib. If this ever doesn't happen, either you're juggling multiple nibs and doing so poorly, or (MUCH less likely) you've found a bug. In the latter case, you should report it (https://bugreport.apple.com/).
Peter Hosey
Turns out I needed to kill all connections and re-do the NIB. That fixed it.I'm amazed that there isn't a programatic way to link these objects together... using a UI tool is too error prone I think and not review-able in the future.