我使用的是一个基于苹果示例代码的简单NSAlert,虽然它显示得很好,但它永远不会消失。
代码:
void DisplayAlert()
{
NSAlert *alert = [[NSAlert alloc] init];
NSLog(@"TEST");
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:@"Yay!"];
[alert setInformativeText:@"This is an informational alert."];
[alert setAlertStyle:NSAlertStyleInformational];
[alert runModal];
NSLog(@"TEST2");
[alert.window close];
[alert release];
NSLog(@"TEST3");
}我已经尝试过使用和不使用[alert.window close]行,但这两种方式都不会使警告消失。
我也试过把第一行写成[[[NSAlert alloc] init] autorelease];,但也没有用。
此时将显示所有NSLog消息。
发布于 2018-08-28 00:04:07
我也遇到了同样的问题,我挣扎了很长一段时间,试图找到一种方法来让被驳回的警报消失。
我的解决方案是完全放弃NSAlert,转而使用CFUserNotificationAlert。This blocking alert API或non-blocking CFUserNotificationNotice API可用于显示与NSAlert生成的警告对话框相同的独立警告对话框,但与从简单的无窗口二进制文件运行时的NSAlert对话框不同,它们可以被清除。
Here's an example of CFUserNotificationDisplayAlert及其部分代码的预览:
#import <CoreFoundation/CoreFoundation.h>
int main(void) {
CFOptionFlags cfRes;
//display alert with 5 second timeout, at NoteAlertLevel
CFUserNotificationDisplayAlert(5, kCFUserNotificationNoteAlertLevel,
NULL, NULL, NULL,
CFSTR("Testing"),
CFSTR("Click on any button..."),
CFSTR("OK"),
CFSTR("Cancel"),
CFSTR("Test Button"),
&cfRes);
return cfRes;
}发布于 2018-02-01 12:27:17
您需要的方法是-orderOut:,而不是-close。警告/面板窗口不是文档,也不是通常意义上的“关闭”。你只是想让他们消失。
https://stackoverflow.com/questions/48550829
复制相似问题