UINavigationControllerDelegate‘s didShowViewController method was called twice

后端 未结 3 1154
情深已故
情深已故 2021-02-19 15:01
class ViewController: UIViewController, UINavigationControllerDelegate {

      override func viewDidLoad() {
         super.viewDidLoad()
         navigationController!         


        
相关标签:
3条回答
  • 2021-02-19 15:32

    didShowViewController is called twice because the first time it is called when the navigation controller transitions to showing the view controller. And then it is called again by the navigation controller's own viewDidAppear when it appears on screen, using the topViewController as the controller param which in this case is the same as the controller the first time it was called.

    0 讨论(0)
  • 2021-02-19 15:39

    I hit the same issue in my code. I was able to work around it by waiting until viewDidAppear to set the navigation delegate instead of setting it in viewDidLoad. To translate it to your example:

    override func viewDidLoad() {
       super.viewDidLoad()
    }
    
    // ...
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        navigationController!.delegate = self
    }
    
    0 讨论(0)
  • 2021-02-19 15:40

    The UINavigationController has displayed two instances of a UIViewController

    From the UINavigationControllerDelegate documentation

    Called just after the navigation controller displays a view controller’s view and navigation item properties.

    Instead of logging "showViewController", log the UIViewController instance to see what's going on

    func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
         print(viewController)
    }
    
    0 讨论(0)
提交回复
热议问题