How to prevent a nib from loading if already in instance is loaded?

随声附和 提交于 2019-12-11 08:55:49

问题



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?

Thanks in Advance...

UPDATE:

@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  

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).


回答1:


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

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;

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];
}


来源:https://stackoverflow.com/questions/4895286/how-to-prevent-a-nib-from-loading-if-already-in-instance-is-loaded

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