[英]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:
到目前为止,我能够观察到的是:
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.