在呈现的视图控制器中的popToRootViewControllerAnimated之后未调用navigationController:willShowViewController:animated:

时间:2018-08-13 14:58:41

标签: ios iphone swift uinavigationcontroller navigation

我有UINavigationController,其中包含多个推送视图控制器。 UPD::上次推送的控制器以模态显示另一个控制器。

此外,我在UINavigationControllerDelegatenavigationController:willShowViewController:animated:,有些逻辑。 UPD::导航控制器是其自己的委托。委托是通过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中搜索答案或类似问题,但什么也没发现,有什么想法吗?

2 个答案:

答案 0 :(得分:2)

在您的“演示” VC中,您想要实现一个委托/协议模式,以便您的“演示” VC可以回叫并执行dismiss和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

答案 1 :(得分:0)

从文档中:  import './css/button.css'; import './css/chat.css'; 弹出堆栈中除根视图控制器以外的所有视图控制器,并更新显示内容

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

因此似乎每次都需要调用popViewControllerAnimated:来调用navigationController:willShowViewController:animated:,因为每次弹出新控制器后显示都会更新。当您弹出到根视图控制器时,更新显示仅被调用一次。