gpt4 book ai didi

objective-c - NSAlert不会消失

转载 作者:行者123 更新时间:2023-12-03 17:18:42 26 4
gpt4 key购买 nike

我正在使用一个基于 Apple 示例代码的简单 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 消息都会出现。

最佳答案

我也遇到了同样的问题,并且挣扎了很长时间,试图找到一种方法来使被忽略的警报消失。

我的解决方案是完全放弃 NSAlert,转而使用 CFUserNotificationAlert。 This blocking alert APInon-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;
}

关于objective-c - NSAlert不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48550829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com