问题描述
我面对这个奇怪的问题,我不知道我会出错。
I'm facing this strange problem and I'm not sure where I'm going wrong.
情况:
我有一个MainWindowController类,它将加载正确的nibs来显示。
我创建了一个对象,并将其更改为MainWindowController类中的IB,并把它与MainWindowViewnib。
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.
所以基本上,nibMainWindowView自定义视图和侧面的几个按钮,以便用户可以选择要加载的视图,并且它将加载另一个相应的笔尖。我在IB启动时隐藏了mainmenu,所以它只加载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 the MainWindowView with the other nib files.
在调试时,当我把一个NSLog在MainWindowController的awakeFromNib ,我看到在控制台它被调用两次。
While debugging however, when I put a NSLog on MainWindowController's awakeFromNib, I saw that in the console it was being called twice.
在我的项目中,我有一个startupController类与MainMenu.xib与以下代码。
in my project, I have a startupController class together with MainMenu.xib with the following code.
startUpController.h:
startUpController.h:
#import <Cocoa/Cocoa.h>
#import "MainWindowController.h"
@interface startupController : NSObject {
MainWindowController *myWindowController;
}
@end
startUpController.m:
startUpController.m:
#import "startupController.h"
@implementation startupController
-(void)awakeFromNib {
[super init];
if(myWindowController == nil)
myWindowController = [[MainWindowController alloc] initWithWindowNibName:@"MainWindowView"];
[myWindowController showWindow:self];
}
@end
任何帮助。感谢。
推荐答案
这听起来像是创建两个MainWindowController对象,所以awakeFromNib将被调用一次。
It sounds like you are creating two MainWindowController objects, so awakeFromNib will be called once for each.
在startupContoller方法中的awakeFromNib是创建一个MainWindowController对象,并用MainWindowView nib初始化它。如果你还添加了一个对象到该nib并将其类型设置为MainWindowController,另一个MainWindowController对象将在加载nib时创建。
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.
你应该删除MainWindowController对象从MainWindowView nib,并改为设置文件的所有者对象的类型为MainWindowController。 (MainWindowController应该是NSWindowController的子类)任何你连接到你创建的对象的连接应该被连接到File的Owner。
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.
此外,你不应该在awakeFromNib中调用[super init]。你应该只在init方法中调用[super init]。
Also, you shouldn't be calling [super init] in the awakeFromNib. You should only be calling [super init] in an init method.
这篇关于View Controller调用awakeFromNib两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!