OSX - Cocoa - 关闭工作表时出现异常

时间:2013-10-24 09:15:02

标签: macos cocoa nswindowcontroller

我正在玩我的应用程序中使用工作表。

我有一个小测试项目设置来找到我的方式。

我确实让这段代码正常工作:

- (IBAction)activateSheet:(id)sender {
    if (!_sheet)
        [NSBundle loadNibNamed:@"Sheet" owner:self];
    [NSApp beginSheet:self.sheet
       modalForWindow:[[NSApp delegate] window]
        modalDelegate:self
       didEndSelector:NULL
          contextInfo:NULL];
}

但不推荐使用loadNibNamed方法。

所以我用这个代替:

- (IBAction)activateSheet:(id)sender {
    if (!_sheet)
        NSLog(@"1");
    [[NSBundle mainBundle] loadNibNamed:@"Sheet" owner:self topLevelObjects:nil];
    [NSApp beginSheet:self.sheet
       modalForWindow: [[NSApp delegate] window]
        modalDelegate:self
       didEndSelector:NULL
          contextInfo:NULL];
}

纸张打开正常。但是,如果我按保存或取消,我会收到EXC_BAD_ACCESS异常:

int main(int argc, const char * argv[])
{
    return NSApplicationMain(argc, argv);
}

更新 - 这是一个轻而易举的10.9:

- (IBAction)activateSheet:(id)sender {
    if (!_sheet)
    [[NSBundle mainBundle] loadNibNamed:@"Sheet" owner:self topLevelObjects:nil];
    [[[NSApp delegate] window] beginSheet:self.sheet completionHandler:nil];    
}



- (void)endSheet:(NSWindow *)sheetWindow returnCode:(NSModalResponse)returnCode {
    [sheetWindow orderOut:self];    
}

- (IBAction)save:(id)sender {
    [self doSave];
    result = 1;
    [self endSheet:self.sheet returnCode:result];   
}

- (IBAction)cancel:(id)sender {
    result = 0;
    [self endSheet:self.sheet returnCode:result];   
}

1 个答案:

答案 0 :(得分:4)

如下所示: -

 [NSApp beginSheet:self.sheet
       modalForWindow: [[NSApp delegate] window]
        modalDelegate:self
       didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
          contextInfo:nil];

    - (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
    {
if (returnCode == NSAlertDefaultReturn)
{
        [sheet orderOut:self];

    }

else if (returnCode == NSAlertAlternateReturn)
{

}