关闭窗口前的Objective-C确认

时间:2014-02-04 23:36:19

标签: objective-c nswindow

我想向用户寻求确认他想要关闭唯一的窗口以及整个应用程序

到目前为止,我有这个:

- (BOOL)windowWillClose:(id)sender{
    NSAlert *alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle:@"Yes"];
    [alert addButtonWithTitle:@"No"];
    [alert setMessageText:@"Are you sure you want to quit?"];
    [alert setInformativeText:@"Quiting will stop the machine, please make sure it is back to its origin."];
    [alert setAlertStyle:NSWarningAlertStyle];
    [alert setShowsSuppressionButton:YES];
    NSInteger result = [alert runModal];

    if ( result == NSAlertFirstButtonReturn ) {
        return YES;
    } else {
        return NO;
    }
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {

     return YES;
}

窗口和应用程序关闭之前有一个ifrt,如果我移除applicationShouldTerminateAfterLastWindowClosed我的窗口关闭,没有任何反应。但是当我把它打开并把这个转发器放在applicationShouldTerminateAfterLastWindowClosed时,这个转发器已经工作但是我的窗户已经关闭了。

我也尝试过windowShouldClose,但这也没有用。

关于我在这里做错了什么想法?

1 个答案:

答案 0 :(得分:5)

添加实现

的委托
- (BOOL)windowShouldClose:(id)sender;

委托方法,请参见此处:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSWindowDelegate_Protocol/Reference/Reference.html

相关问题