gpt4 book ai didi

cocoa - 如果已经加载了实例,如何防止 Nib 加载?

转载 作者:行者123 更新时间:2023-12-03 17:07:02 24 4
gpt4 key购买 nike


我正在开发一个小应用程序。在第一个窗口中,我可以选择创建新帐户。我为此使用“继续”按钮。单击此按钮后,将打开另一个用于创建新帐户的窗口。我希望一旦打开此窗口,该 nib 文件的其他实例就不应再次加载。即使用户再次单击“继续”,已经打开的 nib 文件实例(用于创建新帐户的实例)也应该出现在前面。
是否有任何 API 可以帮助检查 nib 的一个实例是否已加载?

或者可能是提供内存中加载的所有 Nib 列表的东西?

提前致谢...

更新:

@interface WelcomePageController : NSObject {
IBOutlet NSTextField * userNameField;
IBOutlet NSPopUpButton * actionList;

IBOutlet NSWindow * welcomePage;

CreateNewAccountWindowController * createNewAccountWindowController;

}

-(IBAction) changePasswordButton:(id)sender;
-(IBAction) logOutButton:(id)sender;
-(IBAction) continueButton:(id)sender;
@end


@implementation WelcomePageController



-(void)windowDidUpdate:(id)sender{
UserInfo * user=[UserInfo uInfoObject];
[userNameField setStringValue:[user.firstName stringByAppendingFormat:@" %@!", user.lastName]];
if ([user.userType isEqual:@"Standard"]) {
[actionList setAutoenablesItems:NO];
[[actionList itemAtIndex:2]setEnabled:NO];
[[actionList itemAtIndex:3]setEnabled:NO];
}
else {
[actionList setAutoenablesItems:YES];
}

}


-(IBAction) changePasswordButton:(id)sender{
[NSBundle loadNibNamed:@"ChangePassword" owner:self];
}


-(IBAction) continueButton:(id)sender{
if ([actionList indexOfSelectedItem]==0) {
[NSBundle loadNibNamed:@"ViewAvailableItemsWindow" owner:self];
}
else if([actionList indexOfSelectedItem]==1){
[NSBundle loadNibNamed:@"NewOrderPage" owner:self];
}
else if([actionList indexOfSelectedItem]==2){
[NSBundle loadNibNamed:@"ManageItemList" owner:self];
}
else {
if(!createNewAccountWindowController){
createNewAccountWindowController=[[CreateNewAccountWindowController alloc]init];
}
[createNewAccountWindowController showWindow:self];

//[NSBundle loadNibNamed:@"NewAccount" owner:self];
}

}


-(IBAction) logOutButton:(id)sender{
[NSBundle loadNibNamed:@"LoginPage" owner:self];
[[sender window]close];
}
@end

这是我正在使用的完整代码....有问题的代码是方法 continueButton..else 条件(最后一个)..
我已经尝试过了。单击“继续”按钮后,我打开“NewAccountWindow”。我关闭窗口并再次单击继续按钮。但是,这次“NewAccountWindow”不会再次打开(即使已经存在的实例也不会显示)。

最佳答案

标准方法是让 NSWindowController 的子类(可能保存窗口小部件的导出)负责加载 nib 文件。例如,

@interface CreateAccountWindowController : NSWindowController {
// …
}
// …
@end

@implementation CreateAccountWindowController
- (id)init {
self = [super initWithWindowNibName:@"CreateAccount"];
return self;
}
// …
@end

当用户单击“继续”按钮时,您将有一个处理该单击的操作方法。在包含action方法的类中,为相应的窗口 Controller 声明一个实例变量:

CreateAccountWindowController *createAccountWindowController;

并且,在处理“继续”按钮单击的操作方法中,当且仅当尚不存在 CreateAccountWindowController 实例时,才创建一个实例。这将确保在任何给定时间最多存在该窗口 Controller 的一个实例,因此相应的 nib 文件最多加载一次:

- (IBAction)showCreateAccountWindow:(id)sender {
if (! createAccountWindowController) {
createAccountWindowController = [[CreateAccountWindowController alloc] init];
}
[createAccountWindowController showWindow:self];
}

关于cocoa - 如果已经加载了实例,如何防止 Nib 加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4895286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com