简体   繁体   English

在将视图控制器推入导航堆栈之前调用viewDidAppear()

[英]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. 由于某种原因,在应用加载时( 在将其推送到导航堆栈之前)会调用LabelledVCviewDidAppear(_:)方法,这对我来说没有任何意义。 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.

相关问题 iOS:为什么这个视图 Controller 没有被推送到导航控制器的堆栈中? - iOS: Why isn't this View Controller getting pushed onto the Navigation Controller's stack? 如何在情节提要中自定义由标识符实例化并推送到导航堆栈的视图控制器的UINavigationItem? - How to custom in storyboard my view controller's UINavigationItem that is instantiated by identifier and pushed onto navigation stack? 在导航堆栈中找不到Pushed View控制器? - Pushed View controller is not found in the navigation stack? 将新的视图控制器推入堆栈时,状态栏会进行动画处理 - Status bar animating while a new view controller is pushed onto the stack 我的viewDidAppear没有在视图控制器中调用 - My viewDidAppear not called in view controller 推送视图控制器-viewDidAppear未调用 - Pushing View Controller - viewDidAppear not called iOS 开发:如何在将视图控制器推送到导航堆栈之前预加载它? - iOS Development: How can I preload a view controller before pushing it onto the navigation stack? 在将视图控制器推到导航控制器上时旋转不能正确调整推入的视图控制器的大小 - Rotating whilst pushing a view controller onto a navigation controller does not correctly size pushed view controller 如何检查视图控制器是模态呈现还是推送到导航堆栈上? - How to check if a view controller is presented modally or pushed on a navigation stack? 将子视图添加到导航控制器时未调用viewdidappear - viewdidappear not called when adding subview to navigation controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM