[英]viewDidAppear() called before view controller is pushed onto navigation stack
I have a basic navigation setup in my Storyboard: a vanilla UIViewController
embedded in a UINavigationController
. 我的情节提要中有一个基本的导航设置:嵌入在
UINavigationController
的香草UIViewController
。 In my main VC I have two buttons that each segue to a UIViewController
subclass: LabelledVC
. 在我的主要VC中,我有两个按钮,每个按钮都连接到
UIViewController
子类: LabelledVC
。 In the subclass's viewDidAppear(_:)
method I set the navigation item's titleView
to a custom image: 在子类的
viewDidAppear(_:)
方法中,将导航项的titleView
设置为自定义图像:
class LabelledVC: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let logoImage = UIImage(named: "apple")
let logo = UIImageView(image: logoImage)
logo.contentMode = .scaleAspectFit
logo.frame = CGRect(x: 0, y: 0, width: 32, height: 32)
navigationItem.titleView = logo
}
}
For some reason LabelledVC
's viewDidAppear(_:)
method is being called when the app loads ( before it is pushed onto the navigation stack) which doesn't make any sense to me. 由于某种原因,在应用加载时( 在将其推送到导航堆栈之前)会调用
LabelledVC
的viewDidAppear(_:)
方法,这对我来说没有任何意义。 You can find the project here . 您可以在此处找到该项目。
Your MainVC is inherit from LabelledVC. 您的MainVC继承自LabelledVC。 So when application did show this controller the system calling viewDidAppear in ViewController but you don't have implementation for this method, so system call this method from parent class.
因此,当应用程序确实向该控制器显示该系统时,系统会在ViewController中调用viewDidAppear ,但您没有此方法的实现,因此系统会从父类中调用此方法。
One other thing. 另一件事。 For your example best place to configure NavigationItem is viewDidLoad method.
对于您的示例,配置NavigationItem的最佳位置是viewDidLoad方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.