开发者

View Controller calls awakeFromNib twice

I'm facing this strange problem and I'm not sure where I'm going wrong.

Situation:

I have a MainWindowController class which will load the correct nibs to be displayed. I created an object and changed it to MainWindowController class in the IB and put it with "MainWindowView" nib.

So basically the nib "MainWindowView" has the custom view and a few buttons on the side so that the user can select which view to load and it will load the other respective nib. I've hidden the mainmenu during launch in the IB so it only loads开发者_如何学Go the MainWindowView with the other nib files.

While debugging however, when I put a NSLog on MainWindowController's awakeFromNib, I saw that in the console it was being called twice.

in my project, I have a startupController class together with MainMenu.xib with the following code.

startUpController.h:

#import <Cocoa/Cocoa.h>
#import "MainWindowController.h"

@interface startupController : NSObject {
 MainWindowController *myWindowController;
}
@end

startUpController.m:

#import "startupController.h"
@implementation startupController
-(void)awakeFromNib {
 [super init];
 if(myWindowController == nil)
 myWindowController = [[MainWindowController alloc] initWithWindowNibName:@"MainWindowView"];
 [myWindowController showWindow:self];
}
@end

any help is appreciated. thanks.


It sounds like you are creating two MainWindowController objects, so awakeFromNib will be called once for each.

Your awakeFromNib in the startupContoller method is creating a MainWindowController object and initializing it with the MainWindowView nib. If you've also added an object to that nib and set its type to MainWindowController, another MainWindowController object will be created when the nib is loaded.

You should remove the MainWindowController object from the MainWindowView nib, and instead set the File's Owner object's type to MainWindowController. (MainWindowController should be a subclass of NSWindowController) Any connections you are hooking up to the object you created should be hooked up to File's Owner.

Also, you shouldn't be calling [super init] in the awakeFromNib. You should only be calling [super init] in an init method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜