class ViewController: UIViewController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
navigationController!
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.
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
}
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)
}