New NSWindow from application - mission impossible?

馋奶兔 提交于 2019-12-02 10:07:00

Finally i've managed the problem! In the nib editor for PrefWindow I had to do: Set File's owner class to: NSWindowController then connect window IBOutlet from File's owner to my (preferneces) window. After 6 days of unsuccessful attempts, google works.
Anyway, thanks for all your responses and time!

I'd suggest you move the creation of the PrefWindowController to applicationDidFinishLaunching:

I am not sure the application delegate's init method is called. Probably only initWithCoder: gets called when unarchiving the object from the NIB.

- (id) init {
   if ((self = [super init])) {
      ctrl = [[PrefWindowController alloc] init];
   if ([ctrl window] == nil)
      NSLog(@"Seems the window is nil!\n");
   }
   return self;
}

init is way too early in the scheme of things to be trying to test IBOutlets. They will still be nil yet. Not until later on in the object creation process will the nib outlets be "hooked up". The standard method where you can know that everything in the nib file has been hooked up is:

- (void)awakeFromNib {

}

At that point, all of your IBOutlets should be valid (provided they're not purposely referencing an object in a separate, yet-unloaded nib).

If PrefWindowController is a class that will only be used after the user chooses Preferences from the app menu, I would have to disagree with the others and say that I would not create the instance of the PrefsWindowController at all during the initial load. (Your main controller should be able to function independently from the prefs window). If you have a method that is meant to load the preferences window, then when that method is called, you should check to see if the PrefsWindowController instance is nil, and if it is, create it, then proceed to show the prefs window.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!