views:

243

answers:

2

Hi Guys I am trying to display a custom modal dialogue box and am doing it the following way:

EncPasswordSheet is of IBOutlet NSWindow * type.

@implementation EncryptionPasswordSheet

-(id)init

{

return self;

}

  • (void)showCustomDlg:(NSWindow *)window {

    if (!EncPasswordSheet)

    {

    [NSBundle loadNibNamed: @"EncryptionPasswordDlg" owner: self];
    

    }

    [NSApp beginSheet:EncPasswordSheet modalForWindow:window modalDelegate:nil didEndSelector:nil contextInfo:nil];

    [NSApp runModalForWindow: EncPasswordSheet];

    [NSApp endSheet: EncPasswordSheet];

    [EncPasswordSheet orderOut: self];

}

  • (IBAction)getPasswordFromSheet:(id)sender {

    password = [passwordField stringValue];
    

    [NSApp stopModal];

}

  • (IBAction)cancelEncPasswordSheet:(id)sender

{

password = nil;

[NSApp stopModal];

}

@end

The problem is that the dialogue box is getting displayed well and its taking input and on pressing ok or cancel, their respt IBAction methods are getting executed and program is continuing to execute except with one problem: the dialogue box is not getting out of the way i.e its not getting clsed or dissappearing. Please tell where i am going wrong.

Thanks

A: 

I think you should call endSheet from the IBActions called by your OK and Cancel buttons, which I assume are getPasswordFromSheet and cancelEncPasswordSheet :

- (void)showCustomDlg:(NSWindow *)window {
    if (!EncPasswordSheet) {
        [NSBundle loadNibNamed: @"EncryptionPasswordDlg" owner: self];
    }

    [NSApp beginSheet:EncPasswordSheet
      modalForWindow:window modalDelegate:nil
      didEndSelector:nil contextInfo:nil];
}

- (IBAction)getPasswordFromSheet:(id)sender {
    password = [passwordField stringValue];
    [EncPasswordSheet orderOut: self];
    [NSApp endSheet: EncPasswordSheet];
}

- (IBAction)cancelEncPasswordSheet:(id)sender {
    password = nil;
    [EncPasswordSheet orderOut: self];
    [NSApp endSheet: EncPasswordSheet];
}
Ölbaum
Thanks Olbaum for the reply. I tried your method but it did not work out. the problem is i have to use "runModelForWindow" so as to stop the program execution until the user gives an input and presses ok or cancel.
King
You're right, sorry. Please ignore this answer and see my new one.
Ölbaum
A: 

Have you unchecked "Visible At Launch" in Interface Builder for the dialog window? Because if I don't, then the dialog doesn't apppear as a sheet, and doesn't close when done. Try that with your original code.

Ölbaum
yeh tried that trick but didnt work either. Anyways thanks for the reply.
King
That's strange because because I made a test project and got it working. Does your dialog actually appear as a sheet sliding down from the main window? How do you call showCustomDlg:? What window do you pass it?
Ölbaum