简体   繁体   English

在OS X情节提要中通过标识符实例化拆分视图控制器不会加载视图层次结构

[英]Instantiating split view controller by identifier in OS X storyboard does not load view hierarchy

I'm trying to implement a master-detail-detail view in a Swift OS X application. 我正在尝试在Swift OS X应用程序中实现master-detail-detail视图。 The top-level view controller is an NSSplitViewController with three subview controllers. 顶级视图控制器是带有三个子视图控制器的NSSplitViewController。 The master and first detail views are fairly standard with fixed behaviors. 主视图和第一细节视图是具有固定行为的相当标准的视图。 The second detail view needs to change according to the selection in the first detail view. 第二详细视图需要根据第一详细视图中的选择进行更改。

The second detail controller is a simple view controller, which will respond to selection changes in the first detail view by installing a new subview and controller depending on the selection. 第二个细节控制器是一个简单的视图控制器,它将通过根据选择安装新的子视图和控制器来响应第一个细节视图中的选择更改。

The alternate view controllers for the final content are implemented in the same storyboard, not connected to anything by segues; 最终内容的备用视图控制器在同一情节提要中实现,没有通过segue连接到任何内容; the intent is to create them with -[NSStoryboard instantiateControllerWithIdentifier:] and install them as child view controllers and subviews. 目的是使用-[NSStoryboard InstantiateControllerWithIdentifier:]创建它们,并将它们安装为子视图控制器和子视图。

The instantiated view controllers happen to be NSSplitViewControllers. 实例化的视图控制器恰好是NSSplitViewControllers。

Here's the problem: when I instantiate the detail split view controllers and install them, they are empty. 问题出在这里:当我实例化细节拆分视图控制器并安装它们时,它们是空的。 What I have been able to observe so far is: 到目前为止,我能够观察到的是:

  • The instantiated view controllers are of the correct class (a subview of NSSplitViewController); 实例化的视图控制器属于正确的类(NSSplitViewController的子视图);
  • They have no subviews 他们没有子视图
  • They do have splitViewItems, and the splitViewItems are the view controllers I was expecting to see loaded 他们确实有splitViewItems,并且splitViewItems是我期望加载的视图控制器。

Is there something I need to do to fully load/populate the loaded split view controllers? 我需要做些什么来完全加载/填充已加载的拆分视图控制器吗?

TIA, Doug TIA,道格

Found it. 找到了。 I was trying add the instantiated NSSplitViewController's "splitView" as a subview, rather than its "view" (which contains the splitView). 我试图将实例化的NSSplitViewController的“ splitView”添加为子视图,而不是其“视图”(包含splitView)。 Accessing the "view" property loads the view hierarchy; 访问“ view”属性将加载视图层次结构; using the "splitView" did not. 使用“ splitView”没有。

If you add an split view controller to storyboard and delete the connections to the two provided split view items you might add them with addChildViewController: and you have to set Storyboard ID for those views 如果将拆分视图控制器添加到情节提要中并删除与两个提供的拆分视图项的连接,则可以使用addChildViewController将它们添加:并且必须为这些视图设置Storyboard ID。

class SplitViewController: NSSplitViewController {

@IBOutlet weak var mySplitView: NSSplitView!

var leftPane: NSViewController?
var contentView: NSViewController?

override func viewDidLoad() {
    super.viewDidLoad()
    let story = self.storyboard

    leftPane = story?.instantiateController(withIdentifier: "LeftPaneViewController") as! LeftPaneViewController?
    contentView = story?.instantiateController(withIdentifier: "ContentViewController") as! ContentViewController?

    self.addChildViewController(leftPane!)
    self.addChildViewController(contentView!)
}

}

In my case I was calling addSplitViewItem() in the NSSplitViewController subclass inside init?(coder:) , which was too early. 就我而言,我在init?(coder:)内部的NSSplitViewController子类中调用addSplitViewItem() ,还为时过早。 Once I moved the calls to viewDidLoad() , the view hierarchy of each spit view item's view controller was added to the split view controller's view heirarchy and everything worked correctly. 将调用移到viewDidLoad() ,每个唾液视图项目的视图控制器的视图层次结构都添加到了拆分视图控制器的视图层次结构中,并且一切正常。

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

相关问题 在Swift中使用标识符实例化视图控制器 - Instantiating a view controller with identifier in Swift 返回情节提要当前视图控制器标识符 - Return storyboard current view controller identifier 从子视图 Controller 获取容器视图的 Storyboard 标识符 - Get Storyboard identifier of Container View from child View Controller 实例化View Controller时出错 - Error instantiating View Controller 重构 ViewControllers - 错误:Storyboard 都包含具有相同标识符的视图控制器 - Refactor ViewControllers - Error : Storyboard both contain a view controller with same identifier 故事板不包含标识符错误的视图控制器? - Storyboard doesn't contain a view controller with identifier error? '故事板不包含仅在设备上具有标识符的视图控制器 - 'Storyboard doesn't contain a view controller with identifier' only on Device 故事板不包含带有标识符“ goToC”的视图控制器 - Storyboard doesn't contain a view controller with identifier 'goToC' Swift中的“ Storyboard不包含标识为“ TimeController”的视图控制器错误 - 'Storyboard doesn't contain a view controller with identifier 'TimeController'' error in Swift NSInvalidArgumentException原因:'故事板不包含带有标识符'CenterViewController'的视图控制器 - NSInvalidArgumentException reason: 'Storyboard doesn't contain a view controller with identifier 'CenterViewController''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM