2

我们正在开发一个菜单栏项应用程序,我很想编写一个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];

结果如下可视化:

NSPopover 中的 NSAlert

问题是第三个空按钮,帮助按钮和复选框,没有设置显示。如果没有设置它们,关于如何摆脱它们的任何想法?

4

1 回答 1

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
}
于 2013-05-01T14:30:52.790 回答