Swift 4 Call ViewWillAppear after dismissing View Controller
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
husharoonie
Updated on June 04, 2022Comments
-
husharoonie almost 2 years
I would like to call
viewWillAppear
after dismissing a layoverViewController
.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 gettingnull
errors crashing my app. How can i dismiss aoverContext
ViewController
and call theviewWillAppear
method in a correct manner. -
husharoonie about 6 yearsstill not calling it
-
husharoonie about 6 yearsit still doesnt call viewWillAppear when i dismiss
-
Kowboj about 6 yearsDoes 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 about 6 yearsCould you share the project link ?
-
LateNate over 5 yearsCan you elaborate on 'the problem was the Segue' @husharoonie? I have a similar problem. Can't figure out the answer.
-
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 over 5 yearsThanks @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.