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

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

最佳答案

实现如下:-

 [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)
{

}

关于macos - OSX - Cocoa - 关闭工作表时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19561754/

相关文章:

ruby - 当官方方式不起作用时,如何升级 RVM?

node.js - Visual Studio for Mac上的Docker构建错误-当前上下文中没有构建阶段

html - 从 html 中提取表格到命令行中的 excel

mysql - NSArray 从 MySQL 到 NSTableView

cocoa - 如何关闭窗口(卸载 NIB)?

macos - 在 OSX 上运行 xunit.console.runner

objective-c - NSMutableArray 在枚举时发生了变化

cocoa - MVC - Cocoa 界面 - Cocoa 设计模式书

xcode - NSWindowController 显示新窗口

macos - NSWindow beginSheet finishHandler 未调用