gpt4 book ai didi

objective-c - NSAlert beginSheetModalForWindow 不显示警报

转载 作者:行者123 更新时间:2023-12-02 21:16:28 24 4
gpt4 key购买 nike

我的 OS X 应用程序的 ViewController 中有以下代码:

   NSAlert *alert = [NSAlert new];
alert.messageText = @"Connection error";
alert.informativeText = @"You do not appear to be connected to the internet";
[alert addButtonWithTitle:@"Third button"];
[alert addButtonWithTitle:@"Second button"];
[alert addButtonWithTitle:@"Ok"];

[alert beginSheetModalForWindow:[[self view] window] completionHandler:^(NSInteger result) {
NSLog(@"Success");
}];
// [alert runModal];

当这段代码执行时,什么也没有发生。如果我注释掉 beginSheetModalForWindow 行,并取消注释 [alert runModal],则警报会按预期显示。

我在这里做错了什么,它没有显示为工作表?

最佳答案

我想你正试图过早地显示 NSAlert(当窗口正在设置时)尝试添加一个延迟的 perfromselector 看看是否是这种情况

[self performSelector:@selector(delayed) withObject:nil afterDelay:1.0];


-(void)delayed {
NSAlert *alert = [NSAlert new];
alert.messageText = @"Connection error";
alert.informativeText = @"You do not appear to be connected to the internet";
[alert addButtonWithTitle:@"Third button"];
[alert addButtonWithTitle:@"Second button"];
[alert addButtonWithTitle:@"Ok"];

[alert beginSheetModalForWindow:[self.view window] completionHandler:^(NSInteger result) {
NSLog(@"Success");
}];
}

如果是这样,请在窗口加载后尝试显示它,例如

- (void)windowDidLoad {
//code here
}

关于objective-c - NSAlert beginSheetModalForWindow 不显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30329799/

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