UINavigationControllerDelegate的didShowViewController方法被调用了两次 [英] UINavigationControllerDelegate‘s didShowViewController method was called twice
问题描述
class ViewController: UIViewController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
navigationController!.delegate = self
}
func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
print("showViewController")
}
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
print("sss")
}
func update() {
let vc = SecondViewController()
navigationController!.pushViewController(vc, animated: true)
}
}
这是我演示的第一个控制器,在控制台中:
this is the first controller of my demo , and in console:
sss
showViewController
showViewController
两次调用didShowViewController。
我不确定发生了什么
the "didShowViewController" was called twice. I'm not sure what's going on
-----------------一些测试---- ------------------
-----------------some test----------------------
我在控制器的这些方法中添加一些日志:loadView,viewDidLoad,viewWillAppear,viewDidAppear ,以及这些日志的顺序是:
I add some log in these method of controller : loadView,viewDidLoad ,viewWillAppear,viewDidAppear , and the order of these log is:
loadView
viewDidLoad
viewWillAppear
will:<NaviDemo.ViewController: 0x7fe8c9533050>
<NaviDemo.ViewController: 0x7fe8c9533050>
viewDidAppear
<NaviDemo.ViewController: 0x7fe8c9533050>
推荐答案
覆盖 viewDidLoad( ..)
的 UIViewController
类,你应该总是包含 super.viewDidLoad()
,或者您可能会遇到意外行为(例如,视图控制器在预期一次时显示两次)。
When overriding the viewDidLoad(..)
of the UIViewController
class, you should always include the super.viewDidLoad()
, or you might get unexpected behaviour (e.g., a view controller being shown twice when once is expected).
尝试添加 super.viewDidLoad( )
到您的覆盖viewDidLoad(...)
:
override func viewDidLoad() {
super.viewDidLoad()
// delegates
navigationController!.delegate = self
}
您的日志是否仍然显示您的视图控制器显示两次?
Do your log still show your view controller being shown twice?
这篇关于UINavigationControllerDelegate的didShowViewController方法被调用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!