beginSheetModalForWindow -警告窗口消失



在AppDelegate中实现的这段代码有什么问题?警报很快出现在窗口上方,然后消失。alertDidEnd回调方法永远不会被调用。

我已经尝试在Xcode 4.6.1中清理产品并重建它,没有成功。

- (void) alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
    [[alert window] orderOut:self];
    alertResult = returnCode;
    NSLog(@"alertDidEnd called");
}
- (void) showAlert
{
    NSAlert *saveAlert = [[NSAlert alloc] init];
    [saveAlert setAlertStyle:NSWarningAlertStyle];
    [saveAlert setMessageText:messageText];
    [saveAlert setInformativeText:informativeText];
    [saveAlert addButtonWithTitle:defaultButtonTitle];
    [saveAlert addButtonWithTitle:secondButtonTitle];
    [saveAlert beginSheetModalForWindow:[self window]
                        modalDelegate:self
                        didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
                        contextInfo:nil];
}

我回答自己。问题在applicationShouldTerminate方法的其他地方。

- (NSApplicationTerminateReply )applicationShouldTerminate:(NSApplication *) sender
{   if(conditionOK)
        doSomeStuff;
    else    // all changes are not saved
    {   [self showAlert];
        handleButtonClicked;
    }
    return NSTerminateNow;
}

在if语句的else分支中执行showAlert,但也执行"return NSTerminateNow"。应用程序不会等到单击警报中的按钮。它会立即返回。所以我测试了一个尚未发布的回复。

我将修改applicationShouldTerminate方法

- (NSApplicationTerminateReply )applicationShouldTerminate:(NSApplication *) sender
{   if(conditionOK)
        doSomeStuff;
        return NSTerminateNow;
    else
    {   [self showAlert];
        return NSTerminateCancel;
    }
}

alertDidEnd回调方法将测试返回的按钮,完成任务并在必要时发送终止信号。

目前,我还没有解决问题,但我知道问题在哪里。

只是一个问题:beginSheetModalForWindow总是异步的,或者它是异步的,只有在applicationShouldTerminate上下文中?

这是applicationShouldTerminate的最终版本

- (NSApplicationTerminateReply )applicationShouldTerminate:(NSApplication *) sender
{
    if([_buttonSave isEnabled])
    {   if(quitMenuTerminate)
            [self majFile];
        else
        {   [self showAlert];
            return NSTerminateLater;
        }
    }
    return NSTerminateNow;
}
在beginSheetModalForWindow的回调方法alertDidEnd中,我调用了
[[NSApplication sharedApplication] replyToApplicationShouldTerminate:YES];

现在一切都好了

最新更新