我正在我的应用程序中使用工作表。
我有一个小测试项目设置来找到我的方式。
我确实有这个代码工作:
- (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];
}