模态自定义表未关闭

时间:2009-08-27 22:01:17

标签: cocoa modal-dialog

我正在尝试显示自定义模式对话框并按以下方式执行:

EncPasswordSheet是IBOutlet NSWindow *类型。

@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

正在显示对话框并接受输入。在按下“确定”或“取消”时,正在执行相应的IBAction方法并继续执行程序。问题是对话框不会偏离 - 即它没有被关闭或消失。

2 个答案:

答案 0 :(得分:4)

您是否在Interface Builder中为对话框窗口取消选中“Visible At Launch”?因为如果我不这样做,那么对话框不会作为工作表显示,并且在完成时不会关闭。尝试使用原始代码。

答案 1 :(得分:0)

我认为您应该通过“确定”和“取消”按钮调用IBActions调用endSheet,我认为这些按钮为getPasswordFromSheetcancelEncPasswordSheet

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