How this code works? ViewController inherits from another ViewController it's viewDidLoad func

I found a solution for an issue that I was having to get a SwipeGestureRecognizer to work. And this was the solution:

ViewController1

Code Block
class ViewController1: ViewController2 {
// This ViewController
// has access to the functionality
// declared in viewDidLoad() method
// from ViewController2.
}


ViewController2

Code Block
class ViewController2: UIViewController {
   
  override func viewDidLoad() {
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(_:)))
    swipeRight.direction = .right
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(_:)))
    swipeLeft.direction = .left
    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(_:)))
    swipeUp.direction = .up
    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(_:)))
    swipeDown.direction = .down
     
    view.addGestureRecognizer(swipeRight)
    view.addGestureRecognizer(swipeLeft)
    view.addGestureRecognizer(swipeUp)
    view.addGestureRecognizer(swipeDown)
  }
   
  @objc func respondToSwipeGesture(_ gesture : UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
      switch swipeGesture.direction {
      case .right:
        print("swiped right!")
      case .left:
        print("swiped left")
      case .up:
        print("swiped up")
      case .down:
        print("swiped down")
      default:
        return
      }
    }
  }
}


The question is... Is this a valid approach?

The way I understand viewDidLoad is as a method that gets called to perform initializations on that particular ViewController.

According to Apple Docs


This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView() method. You usually override this method to perform additional initialization on views that were loaded from nib files.

That being said, the way I'm wrapping my head around the solution from above, is that when ViewController1 gets pushed onto the stack, it would pretty much be calling two viewDidLoad methods, one from ViewController1 and the other one from ViewController2.

Am I right?
Answered by OOPer in 672321022

Just edited the post to change that. 

Thanks for editing. Seems you know how to write replies. You should better respond when you get answers or comments on your posts.


when ViewController1 gets pushed onto the stack, it would pretty much be calling two viewDidLoad methods, one from ViewController1 and the other one from ViewController2.

Am I right?

Your description is a little bit too difficult for me to understand, but I guess the answer is NO.
When the view of ViewController1 is loaded the method viewDidLoad() of ViewController1 is called.
In your case, you do not define (override) viewDidLoad() in ViewController1, so viewDidLoad() of ViewController2 is inherited to ViewController1 and the inherited viewDidLoad() is called when its view is loaded. (May not when the view controller is pushed.)

So, if you need to implement viewDidLoad() for ViewController1 and still want the effect done by viewDidLoad() of ViewController2, you may need to use super.
Code Block
class ViewController1: ViewController2 {
//...
override func viewDidLoad() {
super.viewDidLoad() //<- Calls `viewDidLoad()` of `ViewController2`
//...
}
//...
}


What is StreamGestureManager?
Just edited the post to change that. StreamGestureManager is not a thing. It got copied over.
Accepted Answer

Just edited the post to change that. 

Thanks for editing. Seems you know how to write replies. You should better respond when you get answers or comments on your posts.


when ViewController1 gets pushed onto the stack, it would pretty much be calling two viewDidLoad methods, one from ViewController1 and the other one from ViewController2.

Am I right?

Your description is a little bit too difficult for me to understand, but I guess the answer is NO.
When the view of ViewController1 is loaded the method viewDidLoad() of ViewController1 is called.
In your case, you do not define (override) viewDidLoad() in ViewController1, so viewDidLoad() of ViewController2 is inherited to ViewController1 and the inherited viewDidLoad() is called when its view is loaded. (May not when the view controller is pushed.)

So, if you need to implement viewDidLoad() for ViewController1 and still want the effect done by viewDidLoad() of ViewController2, you may need to use super.
Code Block
class ViewController1: ViewController2 {
//...
override func viewDidLoad() {
super.viewDidLoad() //<- Calls `viewDidLoad()` of `ViewController2`
//...
}
//...
}


It is a bit difficult to understand what you want to achieve.

But it seems you found a solution.

Great.

So please close now all the other threads you have opened on the same question (and to which you have not replied. Did you read the answers at least ?).

Have a good continuation.
How this code works? ViewController inherits from another ViewController it's viewDidLoad func
 
 
Q