OSX - Cocoa - Exception when closing a sheet

自古美人都是妖i 提交于 2019-12-11 20:07:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!