navigationController:willShowViewController:animated:在弹出之后不调



我已经UINavigationController了几个推送的视图控制器。上级:上次按下的控制器以模式呈现另一个控制器。

另外,我在navigationController:willShowViewController:animated:UINavigationControllerDelegate了一些逻辑。上级:导航控制器是它自己的委托。委托在viewDidLoad方法中设置。

当我尝试从呈现的视图控制器以编程方式关闭所有控制器时,问题出现了:

// Close all controllers in navigation stack
presentingViewController?.navigationController?.popToRootViewController(animated: true)
// Close presented view controller
dismiss(animated: true, completion: nil)

不调用方法navigationController:willShowViewController:animated:。但是当我在没有呈现控制器的情况下做同样的事情时,就会调用它(感谢@donmag例如它工作的项目(。

搜索了 SO 寻找答案或类似问题,但一无所获,有什么想法吗?

在"呈现"VC 中,您希望实现委托/协议模式,以便"呈现"VC 可以回调并执行关闭和 popToRoot...


// protocol for the presented VC to "call back" to the presenting VC
protocol dismissAndPopToRootProtocol {
func dismissAndPopToRoot(_ animated: Bool)
}

// in the presenting VC
@IBAction func presentTapped(_ sender: Any) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "presentMeVC") as? PresentMeViewController {
// Assign the delegate when instantiating and presenting the VC
vc.dapDelegate = self
present(vc, animated: true, completion: nil)
}
}
func dismissAndPopToRoot(_ animated: Bool) -> Void {
// this will dismiss the presented VC and then pop to root on the NavVC stack
dismiss(animated: animated, completion: {
self.navigationController?.popToRootViewController(animated: animated)
})
}

// in the presented VC
var dapDelegate: dismissAndPopToRootProtocol?
@IBAction func dismissTapped(_ sender: Any) {
// delegate/protocol pattern - pass true or false for dismiss/pop animation
dapDelegate?.dismissAndPopToRoot(false)
}

这是一个完整的演示项目:https://github.com/DonMag/CustomNavController

来自文档:popToRootViewControllerAnimated:弹出堆栈上除根视图控制器之外的所有视图控制器并更新显示

popViewControllerAnimated:从导航堆栈中弹出顶视图控制器并更新显示

所以似乎为了每次你必须做后续popViewControllerAnimated:时调用navigationController:willShowViewController:animated:,因为每次弹出新控制器后显示都会更新。弹出到根视图控制器时,仅调用一次更新显示。

相关内容

  • 没有找到相关文章

最新更新