在NSPopover中运行NSAlert

时间:2013-05-01 13:30:20

标签: objective-c cocoa nsalert nspopover

我们正在开发菜单栏项目应用, 我想写一个NSAlert类别,该类别显示NSPopover内的NSStatusItem内的警报。

到目前为止,该类别实现了以下新方法:

- (void) runAsMenuItemPopUpWithCompletionBlock:(NSAlertCompletionBlock)block {

    // Get content view of NSAlert
    NSView *alertContentView = [self.window contentView];

    // Ask the menu item to show the view as a NSPopOver
    [[GFMenuItem sharedInstance] popOverView:alertContentView];

    // (...) Handle response with callback
}

但打开警报

NSAlert *alert = [NSAlert alertWithMessageText:@"Learn more?" defaultButton:@"Learn more" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"Do you want to view detailed information?"];
[alert runAsMenuItemPopUpWithCompletionBlock:nil];

导致以下可视化:

NSAlert in NSPopover

问题是第三个空按钮,帮助按钮和复选框,它们尚未设置显示。如果没有设置它们怎么摆脱它们的想法呢?

1 个答案:

答案 0 :(得分:1)

事实证明,您可以致电[alert layout]来触发手动布局处理。它会隐藏任何未设置显示的按钮!

更正方法:

- (void) runAsMenuItemPopUpWithCompletionBlock:(NSAlertCompletionBlock)block {

    // Trigger the layout processing and get content view of NSAlert
    [self layout];
    NSView *alertContentView = [self.window contentView];

    // Ask the menu item to show the view as a NSPopOver
    [[GFMenuItem sharedInstance] popOverView:alertContentView];

    // (...) Handle response with callback
}