问题
(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