简体   繁体   English

未添加NSSplitViewController子视图

[英]NSSplitViewController Child View Not Added

I've created a simple NSViewController and want to add a split view with just one child view. 我创建了一个简单的NSViewController并想添加一个只有一个子视图的拆分视图。 The split view should be controlled by a NSSplitViewController , because I'd like to use the NSSplitItem 's facilities for collapsing/expanding split items. 拆分视图应由NSSplitViewController控制,因为我想使用NSSplitItem的功能来折叠/扩展拆分项。 After adding a child view controller, the split item is created, but no child view is added to the view tree. 添加子视图控制器后,将创建拆分项,但不会将任何子视图添加到视图树。

override func viewDidLoad() {
    super.viewDidLoad()
    let splitViewController = NSSplitViewController()
    view.addSubview(splitViewController.splitView)

    let myController = MyController(nibName: "MyController", bundle: nil)
    splitViewController.addChildViewController(myController)

    printTree(view)
}

func printTree(view: AnyObject, _ n: Int = 1) {
    if let view = view as? NSView {
        NSLog("\(n): \(view)")
        for child in view.subviews {
            printTree(child, n + 1)
        }
    }
}

Output: 输出:

1: <NSView: 0x618000120140>
2: <NSSplitView: 0x6180001205a0>

Why does the split view have no child view? 为什么拆分视图没有子视图?


To compare, here's the version without split view: 作为比较,这是不带分割视图的版本:

override func viewDidLoad() {
    super.viewDidLoad()
    let myController = MyController(nibName: "MyController", bundle: nil)
    view.addSubview(myController.view)
    printTree(view)
}

Output: 输出:

1: <NSView: 0x6100001203c0>
2: <NSView: 0x6000001208c0>  <-- here's my child view
3: <NSButton: 0x600000140580>

And adding the child view directly as a subview to the split view doesn't work either: 并且直接将子视图作为子视图添加到拆分视图中也不起作用:

A SplitView managed by a SplitViewController cannot have its subviews modified

So, my question is, why is the child view not added to the view tree inside the split view? 所以,我的问题是,为什么子视图未添加到拆分视图内部的视图树中?

"You're doing it wrong" “你这样做是错的”

You're using base class methods when NSSplitViewController has a very particular API. 当NSSplitViewController具有非常特殊的API时,您正在使用基类方法。

See: https://developer.apple.com/library/prerelease/mac/samplecode/Exhibition/Listings/Exhibition_GalleryWindowController_swift.html for an example. 有关示例,请参阅: https//developer.apple.com/library/prerelease/mac/samplecode/Exhibition/Listings/Exhibition_GalleryWindowController_swift.html

You want the addSplitViewItem: method. 您需要addSplitViewItem:方法。

I figured it out. 我想到了。 My mistake was that I added the splitView instead of the view : 我的错误是我添加了splitView而不是view

// this won't work:
self.view.addSubview(splitViewController.splitView)

// this will work:
self.view.addSubview(splitViewController.view)

BTW: using splitViewController.addChildViewController(myController) as I did before is just a shorter way of saying the following: 顺便说一句:像我以前那样使用splitViewController.addChildViewController(myController)只是一种简短的说法:

let item = NSSplitViewItem(viewController: myController)
splitViewController.addSplitViewItem(item)

which didn't work for me because of my mistake described above. 由于上述错误,这对我不起作用。

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

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