开发者

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

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)send开发者_StackOverflow中文版er;
@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).


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜