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
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.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.
Am I right?
Thanks for editing. Seems you know how to write replies. You should better respond when you get answers or comments on your posts.Just edited the post to change that.
Your description is a little bit too difficult for me to understand, but I guess the answer is NO.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?
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` //... } //... }