问题
I am playing with using a sheet in my app.
I have a little test project setup to find my way.
I did have this code working:
- (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];
}
but the loadNibNamed method is deprecated.
So I substitute this:
- (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];
}
The sheet opens fine. But if I press save or cancel, I get a EXC_BAD_ACCESS exception:
int main(int argc, const char * argv[])
{
return NSApplicationMain(argc, argv);
}
UPDATE - This is a breeze with 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:
implement like below:-
[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)
{
}
来源:https://stackoverflow.com/questions/19561754/osx-cocoa-exception-when-closing-a-sheet