0

I was told that awakeFromNib should be called only once so I was quite surprised to see it was called twice for my WindowController. I use IB to create my WindowController in MainMenu.xib, but to tell it what window xib file to load I added an init method to it.

When I debug the code, I find the first call to awakeFromNib triggered by this call stack. So basically [MyWindowController init] triggered the first call. Then the second call I guess was because of loading MainMenu.xib. Is that correct ?

Can someone cast some light on it?

BTW, I read some other questions similar to mine on stack overflow and some answer said it may because of file owen issue. So I particularly change the file owner of the window xib file to NSApplication. But awakeFromNib was still called twice for MyWindowController.

enter image description here

Qiulang
  • 10,295
  • 11
  • 80
  • 129
  • Are you sure you don't have two WindowController instances? – Lily Ballard Sep 21 '11 at 07:40
  • I don't think there is any chance that 2 windowcontroller instances will be created b/c I only create 1 in my MainMenu.xib. But I do check that by printing out its address using %P in NSLog. And that confirm that only 1 instance is created. – Qiulang Sep 21 '11 at 08:13

1 Answers1

1

From your question I understand that you are instantiating MyWindowController in IB and then in MyWindowControllers init you are loading a second nib with initWithWindowNibName:? If so, you will naturally receive two awakeFromNib calls. One when loading the NIB specified in your init method, the other when loading MainMenu.xib.

Michael
  • 447
  • 3
  • 10
  • Then I have another question, where should I put my init code for WindowController after it loads its own nib ? I have some attributes to set after loading the nib and I put them in awakeFromNib currently. But if awakeFromNib is called twice is there a better place to put my init code ? – Qiulang Sep 21 '11 at 14:22
  • 1
    You could override NSWindowControllers windowDidLoad method. EDIT: The window loading sequence is not done at this point, so don't try running sheets on the window or similar. – Michael Sep 24 '11 at 14:16