简体   繁体   English

NSTabViewController 添加 NSToolbarItems

[英]NSTabViewController add NSToolbarItems

I'd like to use the NSTabViewController for switching through 6 different Tabs with the toolbar style.我想使用 NSTabViewController 来切换带有工具栏样式的 6 个不同的选项卡。

All tabs have in common that they show different aspects of a Customer entity.所有选项卡的共同点是它们显示客户实体的不同方面。

Now I want to add aditional NSToolbarItems to the toolbar of the NSTabViewController?现在我想在 NSTabViewController 的工具栏中添加额外的 NSToolbarItems 吗? But I haven't found a way to access the toolbar.但是我还没有找到访问工具栏的方法。 I also would like to add Space between the ToolbarItems.我还想在 ToolbarItems 之间添加空间。

Is there a way to do so?有没有办法这样做?

Or how can I add my ViewController from the Storyboard to a NSTabView without using NSTabViewController?或者如何在不使用 NSTabViewController 的情况下将我的 ViewController 从 Storyboard 添加到 NSTabView?

Regards问候

Oliver奥利弗


In the meantime I've tried another approach that I thought was more promising but lead to another strange behaviour:与此同时,我尝试了另一种我认为更有希望但导致另一种奇怪行为的方法:

I've created a new NSViewController and put a NSTabView inside.我创建了一个新的 NSViewController 并在里面放了一个 NSTabView。 In order to load my already existing ViewControllers I used this为了加载我已经存在的 ViewControllers,我使用了这个

override func viewDidLoad() {
    super.viewDidLoad()
    let customerController = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("CustomerVCID")) as! CustomerViewController

    let servicesController = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("ServicesVCID")) as! ServicesController

    customerController.customer = self.customer
    servicesController.customer = self.customer

    self.tabView.tabViewItems[0].view = customerController.view
    self.tabView.tabViewItems[1].view = servicesController.view

}

That indeed worked, but now all my NSButtons that have actions will cause my application to crash.这确实有效,但现在我所有具有操作的 NSButton 都会导致我的应用程序崩溃。

There is only one toolbar per window.每个窗口只有一个工具栏。 So your NSTabViewController shares it.所以你的NSTabViewController共享它。

  1. Select toolbar mode of NSTabViewController选择NSTabViewController工具栏模式
  2. Override NSWindowController and add your items覆盖NSWindowController并添加您的项目

Example:例子:

  override func windowDidLoad() {
      super.windowDidLoad()
      window?.toolbar?.insertItem(withItemIdentifier: .print, at: 0)
  }

You can always access your toolbar via following path view->window->toolbar您始终可以通过以下路径视图->窗口->工具栏访问您的工具栏

Your only issue is that there is one delegate per NSToolbar .您唯一的问题是每个NSToolbar有一个delegate Which means you have to create your custom NSToolbarItem inside NSTabViewController delegate.这意味着您必须在NSTabViewController委托中创建您的自定义NSToolbarItem

   override func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        if itemIdentifier == .export {
            return ExportToolbarItem.new()
        } else {
            return super.toolbar(toolbar, itemForItemIdentifier: itemIdentifier, willBeInsertedIntoToolbar: flag)
        }
    }

Remember your are required to call super.请记住,您需要调用 super。 This is because underlying method wants to create bindings to view controller.这是因为底层方法想要创建到视图控制器的绑定。 In case you need actionable buttons in toolbar just add them without calling super.如果您需要工具栏中的可操作按钮,只需添加它们而无需调用 super。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM