开发者

NSAlertPanel not working when Application is quitting

开发者 https://www.devze.com 2023-02-23 12:08 出处:网络
Im trying to allow the user to decide whether to quit the application or not and Ive been trying to implement it using this:

Im trying to allow the user to decide whether to quit the application or not and Ive been trying to implement it using this:

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
int answer = NSRunAlertPanel(@"Quit", @"Are you sure?", @"Quit", @"Cancel", nil);
if (answer == NSAlertDefaultReturn) { return NSTerminateNow;
} else { return NSTerminateCancel;
}
}

I have placed this in my AppDelegate.m and linked the delegate to my main window in interface builder. When i debug and run the application in Xcode, and press the close button, the app window closes but the alert panel does not pop up..

Am i doing something wrong? thank you for your he开发者_开发百科lp!! I am new here and I hope someone can help me with this.. Thank you so much!


By default closing a window will not quit the application, thus not triggering your alert panel. To make the application quit and show your alert when the user closes the window just add this to the delegate:

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)aApplication {
     return YES;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号