Home > Blockchain >  Swift: Make UIViewController print off the subclass type upon hitting the viewWillAppear or viewWill
Swift: Make UIViewController print off the subclass type upon hitting the viewWillAppear or viewWill

Time:11-20

The title basically asks my question for me. I have a number of view controllers which I'm loading in or dismissing as a user goes through the app. I'm having some asynchrony issues with the dismiss calls being called potentially too much. It's been a bit of a doozy going through the code base and finding what is being called where. I'd like to just print off whenever a new UIViewController hits the points in it's lifecycle functions viewWillAppear() or viewWillDisappear(). Is there a way for me to extent the UIViewController in such a way that all of its subclasses will naturally do this? Or would I have to go through each subclass to add in that code?

Thank you!

CodePudding user response:

You could create a new UIViewController base class with the following:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    print("\(type(of: self)) will appear")
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    print("\(type(of: self)) will disappear")
}

You'll have to inherit from this class on any view controller whos events you want to log

  • Related