【问题标题】:Show NSPopover from NSToolbarItem Button从 NSToolbarItem 按钮显示 NSPopover
【发布时间】:2011-11-15 20:33:49
【问题描述】:

我想在我的工具栏中显示来自NSToolbarItem 按钮的NSPopover
(即位于按钮下方)。

理想情况下,我想将按钮的NSView 传递给弹出框以定位它。
我的问题是,如何获得NSToolbarItemNSView

[toolbarbutton view] 总是返回 nil。

【问题讨论】:

    标签: cocoa nsview nstoolbaritem


    【解决方案1】:

    答案似乎在 2011 年 WWDC 第 113 场“全屏和 Aqua 变化”的视频中。基本上,在 NSToolbaritem 中放置一个 NSButton 并使用它的视图。

    一篇博文在这里:http://www.yellowfield.co.uk/blog/?p=33,一个示例项目在 github 上http://github.com/tevendale/ToolbarPopover

    一切以http://xkcd.com/979为精神!

    【讨论】:

      【解决方案2】:

      您可以直接从包含在NSToolbarItem 中的NSButton 发送操作(无论如何,您通常应该这样做,考虑分段控件,其中每个分段都有自己的目标/操作),这样就可以诡计。

      【讨论】:

      • 谢谢 - 我会试试看。
      【解决方案3】:

      不要从 IBAction 发送方获取视图,而是将 IBOutlet 直接连接到工具栏项并使用它来获取相对视图:

      在你的头文件中:

      @property (weak) IBOutlet NSToolbarItem *theToolbarItem;
      @property (weak) IBOutlet NSPopover *thePopover;
      

      在您的实现文件中,显示弹出框:

      [self.thePopover showRelativeToRect:[[self.theToolbarItem view] bounds] ofView:[self.theToolbarItem view] preferredEdge:NSMinYEdge];
      

      这也适用于从工具栏项内的菜单项选择中显示弹出窗口。

      【讨论】:

        【解决方案4】:

        虽然我确实使用 Stuart Tevendale 提到的方法显示了 Popover,但当我尝试使用 NSToolbarDelegate 验证(启用/禁用)NSToolbarItems 时确实遇到了问题:

        -(BOOL)validateToolbarItem:(NSToolbarItem *)toolbarItem {
            BOOL enable = YES;
        
            NSString *identifier = [toolbarItem itemIdentifier];
        
            // This does never get called because I am using a button inside a custom `NSToolbarItem`
            if ([identifier isEqualToString:@"Popover"]) {
                return [self someValidationMechanism];
            } 
        
            // For this the validation works when I am using a standard `NSToolbarItem`
            else if ([identifier isEqualToString:@"StandardToolbarItem"]){
                return [self someOtherValidationMechanism];
            }
        
            return enable;
        }
        

        所以我建议不要显示来自NSToolbarItem 的弹出框。另一种方法可能是显示页面表:How to show a NSPanel as a sheet

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-05
          相关资源
          最近更新 更多