NSAlert beginSheetModalForWindow不显示警报

时间:2015-05-19 15:23:59

标签: objective-c macos nsalert

我在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],则会按预期显示警报。

我在这里做错了它没有显示为表格?

1 个答案:

答案 0 :(得分:4)

我想你试图过早地显示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
}