I have a Custom class which is supposed to load a window from a nib file.
When I load the nib file everything looks fine except the IBOutlets are not connected i.e. nil. IBActions work fine and when they are called the IBOutlets are not nil anymore.
The class is added to the nib in the IB as an object and obviously everything wired up.
It’s the file’s Owner and delegate
When it loads the nib, the window appears only if its “visible at launch” is set.
It doesn’t matter where I load the nib and try to access IBOutlets immediately or seconds later.
It must be something very trivial…
UPDATE2: I UPLOADED AN EVEN SIMPLER TRIAL PROJECT: Trial Project2
Expected behaviour: Window2 title changes to “Title has Changed x times” when loads. It only starts working once the button is pressed i.e. IBOutlets are not nil anymore.
I haven’t worked with any of the OS X interface classes, so there may be some aspect of this that is not 100% precisely accurate, but basically what is happening is this:
You’ve wired your nib’s
NSWindowobject to aMyClassobject, which is also in your nib. So when you load that nib, here’s what’s happening:MyClassinstance is createdNSWindowinstance is created, with several subviews. TheNSWindowand the button are attached to the newMyClassinstance.MyClassinstance you created in your app delegate)Then
-changeWindowTitleis called on your originalMyClassinstance, which has none of its outlets wired.The solution is simple: remove the MyClass object from your nib file. Select the “File’s Owner”, and in the Identity Inspector (third icon from the left in the Utility pane) set “Class” to “MyClass”. Now reconnect your outlets to the File’s Owner object, which is your original
MyClassinstance. You should now see the behavior you expected.As an aside, the right place to do things “as soon as the nib is loaded”, like setting properties on your fresh IBOutlet objects, is in the method
-windowDidLoad.