swift - 从父 View 控制 NSTabViewController

标签 swift cocoa nsviewcontroller nstabview

(我正在使用 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/

相关文章:

ios - 无法调用 Spritekit 的 update() 函数中的函数

ios - 如何用应用商店中的新版本替换 iOS 应用中预先填充的核心数据?

swift - Xcode 9.3 (Swift 4.1) 中的 Codable 'has no initializers'

macos - Cocoa 绑定(bind) - 收到 "deallocated while key value observers were still registered"警告

cocoa - 恢复时自动调整窗口位置

objective-c - NSMutableArray 为 null

ios - 将打印重定向到文件,例如将 NSLog 重定向到文件

objective-c - 为什么 NSRegularExpression 不像 iOS 那样包含在 Mac 应用程序的 foundtion.framework 中?

macos - 使用 nextKeyView 在 NSTextField 之间切换

macos - 绑定(bind) NSViewController 的representedObject