【问题标题】:Running NSAlert within NSPopover在 NSPopover 中运行 NSAlert
【发布时间】:2013-04-25 12:06:33
【问题描述】:

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

产生以下可视化效果:

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

【问题讨论】:

  • 如果您遇到的行为与文档所说的相矛盾(在这种情况下,文档明确表示为 otherButton: 参数提供 nil 应该会导致该按钮不被创建)那么你应该向 Apple 提交错误。
  • 似乎不是真正的错误,请参阅我的答案。还是谢谢!

标签: objective-c cocoa nsalert nspopover


【解决方案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
}

【讨论】:

  • 使用 NSPopover 甚至需要 OS X 10.7。
猜你喜欢
  • 2015-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-05
  • 1970-01-01
相关资源
最近更新 更多