如果已经加载实例,如何防止笔尖加载? [英] How to prevent a nib from loading if already in instance is loaded?

查看:94
本文介绍了如果已经加载实例,如何防止笔尖加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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


i am developing a small application. On the first window, i have an option to create a new account. I use a button "Continue" for this. When this button is clicked, another window for creating a new account opens. I want that once this window is opened, no other instance of this nib file should load again. Even if the user clicks on "Continue" again, already opened instance of the nib file (the one for creating a new account) should come to front.
Is there any API which will help to check if one instance of nib is already loaded?

也许是列出内存中所有笔尖的列表的东西?

Or may be something that gives a list of all the nibs loaded in the memory?

预先感谢...

更新:

@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"不会再次打开(即使已经存在的实例也不会显示).

This is the complete code that i am using....The code in question is the method continueButton..The else condition(last one)..
I have tried this. I open the NewAccountWindow once i click on the Continue button. I close the window and click on the continue button again. However this time the "NewAccountWindow" does not open again(even the already existing instance does not show up).

推荐答案

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

The standard approach for this is to have a subclass of NSWindowController (potentially holding outlets to the window widgets) responsible for loading the nib file. For instance,

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

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

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

When the user clicks the Continue button, you have an action method that handles that click. In the class that contains the action method, declare an instance variable for the corresponding window controller:

CreateAccountWindowController *createAccountWindowController;

,并且在处理继续"按钮单击的操作方法中,如果且仅当尚不存在时,创建CreateAccountWindowController的实例.这样可以确保在任何给定时间最多存在该窗口控制器的一个实例,因此相应的nib文件最多可以加载一次:

and, in the action method that handles clicks of the Continue button, create an instance of CreateAccountWindowController if and only if none exists yet. This will make sure at most one instance of that window controller exists at any given time, hence the corresponding nib file is loaded at most once:

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

这篇关于如果已经加载实例,如何防止笔尖加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆