Swift 4 Call ViewWillAppear after dismissing View Controller

13,218

Solution 1

viewWillAppear is called automatically when you dismiss VC2. Delete: ViewController1().viewWillAppear(true)

Try deleting:

super.viewWillAppear(animated) in VC1. viewDidAppear not getting called

Does it even go back to your VC? self.dismiss works with "Present Modally" segue here. Or embed in NavigationBar, with popViewController

Solution 2

// Override this function in ViewController1
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

 //Your code here will execute after viewDidLoad() or when you dismiss the child viewController  

}

I'd suggest you go through the life cycle of ViewController. Apple documentation for ViewController life cycle

Share:
13,218
husharoonie
Author by

husharoonie

Updated on June 04, 2022

Comments

  • husharoonie
    husharoonie almost 2 years

    I would like to call viewWillAppear after dismissing a layover ViewController.

    ViewController1 -> Segue -> ViewController2

    In VeiwController2

    1.)

    self.dismiss(animated: true, completion: nil)
    

    2.)

     override func viewDidDisappear(_ animated: Bool) {
    
           ViewController1().viewWillAppear(true)
    
        }
    

    In VeiwController1

    When viewWillAppear is called im getting null errors crashing my app. How can i dismiss a overContext ViewController and call the viewWillAppear method in a correct manner.

  • husharoonie
    husharoonie about 6 years
    still not calling it
  • husharoonie
    husharoonie about 6 years
    it still doesnt call viewWillAppear when i dismiss
  • Kowboj
    Kowboj about 6 years
    Does it even go back to your VC? self.dismiss works with "Present Modally" segue here (change the segue). Or embed in NavigationBar, with popViewController. Then view will appear.
  • Tushar Katyal
    Tushar Katyal about 6 years
    Could you share the project link ?
  • LateNate
    LateNate over 5 years
    Can you elaborate on 'the problem was the Segue' @husharoonie? I have a similar problem. Can't figure out the answer.
  • Kowboj
    Kowboj over 5 years
    @LateNate if you push to another controller you can go back by typing 'navigationController?.popViewController(animated: true)'. Self.dismiss doesn't work. Self.dismiss works with modal presentation style 'present(secondController, animated: true)'
  • LateNate
    LateNate over 5 years
    Thanks @Kowboj. I found a less than ideal solution to this myself using NSNotificationCenter. Rather than using ViewWillAppear to update after dismissing I used a notification observer. Didn't want to do it that way, but it works.