正确释放嵌入在子视图中的 NSViewController

Correct release NSViewController embedded in subviews

我的任务是编写一个小应用程序,其中必须将各种控制器嵌入到默认控制器中。所有控制器存储在一个故事板中。

在子视图中嵌入控制器的示例代码


    if let id = getControllerId(pageIndex) { // get controller's storyboard id by segmented index
      let storyBoard = NSStoryboard(name: "Main", bundle: nil)
      let controller = storyBoard.instantiateControllerWithIdentifier(id) as! NSViewController
      controller.view.translatesAutoresizingMaskIntoConstraints = false
      if self.view.subviews.count > 0 {
        let prevView = self.view.subviews[0]
        prevView.removeFromSuperview() // here should be releasing previous controller
      }
      self.view.addSubview(controller.view)
      // make all side constraints
      let views = ["view": controller.view]
      self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-(0)-[view]-(0)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
      self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-(0)-[view]-(0)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
    } else {
      NSLog("ERROR: Unable get controller storyboard id for index \(pageIndex)")
    }

而且我注意到嵌入式控制器不执行 viewWillDisappear。我需要这个事件来清除观察者和其他一些东西。

我不确定将控制器显示为嵌入在子视图中的正确方法,但我没有找到任何其他解决方案。

我制作示例项目来测试这种情况

https://github.com/avvensis/embeddedviewcontrollers

谁能帮我解决这个问题?

viewWillDisappear 不会执行,因为您没有隐藏任何内容。

你的控制者在你创造他之后很快就会死去。所以第 1 步是对其进行引用:

  class ViewController: NSViewController {

// MARK: - Custom properties

let pageIds: [String] = ["redController", "yellowController", "greenController"]

var currentControler : NSViewController!

...

private func showEmbeddedController(pageIndex: Int) {
if let id = getControllerId(pageIndex) { // get controller's storyboard id by segmented index
  let storyBoard = NSStoryboard(name: "Main", bundle: nil)
  currentControler = storyBoard.instantiateControllerWithIdentifier(id) as! NSViewController
  currentControler.view.translatesAutoresizingMaskIntoConstraints = false
  if self.view.subviews.count > 0 {
    let prevView = self.view.subviews[0]
    prevView.removeFromSuperview() // here should be releasing previous controller
  }
  self.view.addSubview(currentControler.view)
  // make all side constraints
  let views = ["view": currentControler.view]
  self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-(0)-[view]-(0)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
  self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-(0)-[view]-(0)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
} else {
  NSLog("ERROR: Unable get controller storyboard id for index \(pageIndex)")
}
}


}

然后在你的基本控制器中使用 deinit 方法:

 class EmbeddedViewController: NSViewController {

.....

deinit {
    print("DEBUG: \(self.className) deinit")
}

}