Control a NSTabViewController from parent View

a 夏天 提交于 2019-12-10 18:35:20

问题


(I'm using storyboards and swift.)

I currently have a NSWindowController which has a NSTabViewController as contentViewController. I am now trying to change the Tabs from the NSWindowController via code using

let tabController = self.contentViewController as! NSTabViewController
tabController.tabView.selectTabViewItemAtIndex(1)

but the TabView doesn't change the view.

Am I using the API wrong?


回答1:


I found an forum post about this topic. There seems to be a bug when setting the tabView index in combination with using NSTabViewController.

Their solution is to select the tab via the controller itself instead of its tabView. This can be done by using the selectedTabViewItemIndex property.

So your code would be the following:

if let contentViewController = self.contentViewController as? NSTabViewController {
    contentViewController.selectedTabViewItemIndex = 1
}

I am going to file a radar on this and you should do the same. This really seems to be a bug, not a misuse of the API.

Update 11/10/2016:
The issue seems to be fixed partly. (macOS Sierra 10.12.1) The method now selects the tab, but the selection in the tab bar does not update..

Update 11/16/2016:
According to Apple, this is not a bug but intended behaviour. They are updating the documentation.

When using a tabViewController, the controller owns the tab UI (segmented control, toolbar, etc.) not the tabView itself. In fact, the tabView has the no tabs style so it only handles switching between content views.

We are using internal bug report 29184908 in order to track updating documentation stating that selectedTabViewItemIndex should be used exclusively in this context.



来源:https://stackoverflow.com/questions/37005622/control-a-nstabviewcontroller-from-parent-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!