(我正在使用 Storyboard和 swift。)
我目前有一个 NSWindowController,它有一个 NSTabViewController 作为 contentViewController。 我现在正尝试通过使用
的代码更改 NSWindowController 中的选项卡let tabController = self.contentViewController as! NSTabViewController
tabController.tabView.selectTabViewItemAtIndex(1)
但 TabView 不会改变 View 。
我使用的 API 有误吗?
最佳答案
我找到了一个 forum post关于这个话题。结合使用 NSTabViewController
设置 tabView
索引时似乎存在错误。
他们的解决方案是通过 Controller 本身而不是它的 tabView 选择选项卡。
这可以通过使用 selectedTabViewItemIndex
属性来完成。
所以你的代码如下:
if let contentViewController = self.contentViewController as? NSTabViewController {
contentViewController.selectedTabViewItemIndex = 1
}
我将对此进行雷达归档,您也应该这样做。这看起来确实是一个错误,而不是 API 的滥用。
2016 年 11 月 10 日更新:
该问题似乎已部分解决。 (macOS Sierra 10.12.1) 该方法现在选择选项卡,但选项卡栏中的选择不会更新。。
2016 年 11 月 16 日更新:
根据 Apple 的说法,这不是错误,而是预期的行为。他们正在更新文档。
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.
关于swift - 从父 View 控制 NSTabViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37005622/