UINavigationController 在使用新 ViewController 更新视图时必须调用方法

UINavigationController has to call method when updates view with a new ViewController

我有一个 UINavigationControllerSubclass。当视图控制器弹出到某个新的视图控制器时(通过 navigationController.popViewControllernavigationController.popToRootViewController 甚至 manually sliding from left to right

我需要在我的导航控制器中调用:

viewController.newTopViewController.updateBackButtonTitle()

实现该目标的最佳方法是什么?

当您return到viewController调用此viewWillAppear方法时。在那个函数里面你可以检查你的 rootviewController 然后你可以调用你的

updateBackbuttonTitle()< function.

您可以使用 viewWillAppear 方法,轻松更新 UI 控件 super.viewWillAppear(动画)

一种方法如下:

class CustomNavigationController: UINavigationController {

    override func popToRootViewController(animated: Bool) -> [UIViewController]? {
        shouldUpdateBackButtonTitle()
        return super.popToRootViewController(animated: animated)
    }

    override func popViewController(animated: Bool) -> UIViewController? {
        shouldUpdateBackButtonTitle()
        return super.popViewController(animated: animated)
    }

    private func shouldUpdateBackButtonTitle() {
        viewController.newTopViewController.updateBackButtonTitle()
    }
}