【问题标题】:NSAlert beginSheetModalForWindow does not display alertNSAlert beginSheetModalForWindow 不显示警报
【发布时间】:2015-07-31 12:39:43
【问题描述】:

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

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

【问题讨论】:

    标签: objective-c macos nsalert


    【解决方案1】:

    我想您正试图过早地显示NSAlert(在设置窗口时)尝试添加一个延迟选择器以查看是否是这种情况

    [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
    }
    

    【讨论】:

    • 就是这样。谢谢!!
    • 它也对我有用,我正在从 awakefromnib 尝试它,但我相信我应该从 windowdidload 调用它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多