隐藏模式视图控制器而不关闭(更改位置)



我在模态控制器中有一些任务,我需要在打开另一个对象时,只替换控制器中的一些元素。我在iOS中发现了以下示例,如何向下拖动以关闭模态?,一切正常,但是我需要确保在关闭控制器时隐藏并且不触发事件deinit。我想有两个事件:解雇和隐藏。

你应该做的是保留 viewController 的内存引用。

例如:

class ParentViewController: UIViewController {
// place this here to keep it in ParentViewController's memory
var subViewController: SubViewController?
override func viewDidLoad() {
super.viewDidLoad()
// initialize the subViewController and set it as the attribute
self.subViewController = SubViewController()
}
func showSub() {
if let unwrappedSubViewController = self.subViewController {
self.present(unwrappedSubViewController, animated: true, completion: nil)
}
}
func dismissSub() {
self.subViewController?.dismiss(animated: true, completion: nil)
}
}
class SubViewController: UIViewController {
.. some properties here
}

只要您的父视图控制器存在subViewController的内存引用存在,因此它就不会被取消/释放

如果要完全删除引用,请执行此操作。

// use optional binding for safety
self.subViewController?.dismiss(animated: true) { [weak self]
self?.subViewController = nil
}

最新更新