gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:31:32 24 4
gpt4 key购买 nike

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

我有一些测试项目设置来找到我的方法。

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

- (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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com