gpt4 book ai didi

ios - 导航 Controller :willShowViewController:animated: is not called after popToRootViewControllerAnimated in presented view controller

转载 作者:可可西里 更新时间:2023-11-01 01:08:33 25 4
gpt4 key购买 nike

我有 UINavigationController 和几个推送的 View Controller 。UPD:最后按下的 Controller 模态呈现另一个 Controller 。

另外,我在 navigationController:willShowViewController:animated: 中有一些逻辑 UINavigationControllerDelegateUPD:导航 Controller 是它自己的委托(delegate)。委托(delegate)在 viewDidLoad 方法中设置。

问题 当我尝试从呈现的 View Controller 中以编程方式关闭所有 Controller 时出现:

// Close all controllers in navigation stack
presentingViewController?.navigationController?.popToRootViewController(animated: true)

// Close presented view controller
dismiss(animated: true, completion: nil)

方法 navigationController:willShowViewController:animated: 未被调用。但是当我在没有 Controller 的情况下做同样的事情时它会被调用(感谢@donmag 例如它工作的项目)。

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

最佳答案

在您的“呈现”VC 中,您想实现委托(delegate)/协议(protocol)模式,以便您的“呈现”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

关于ios - 导航 Controller :willShowViewController:animated: is not called after popToRootViewControllerAnimated in presented view controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51825238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com