Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift viewWillAppear not being called after dismissing view controller

Tags:

ios

swift

I am presenting a view controller from a view controller called HomeController like so:

let viewController = self.storyboard?.instantiateViewController(withIdentifier: "LoginController") as! LoginController  let navigationController: UINavigationController = UINavigationController(rootViewController: viewController)  present(navigationController, animated: true, completion: nil) 

In the presented view controller LoginController at some point gets dismissed:

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

But when it comes back to HomeController it is not calling viewWillAppear, I really need to check on condition on HomeController when it comes back to it, So how can I call viewWillAppear when LoginController dismisses the view?

like image 744
user979331 Avatar asked Jun 28 '18 18:06

user979331


People also ask

Why does viewWillAppear not get called when an app comes back from the background?

In other words, if someone looks at another application or takes a phone call, then switches back to your app which was earlier on backgrounded, your UIViewController which was already visible when you left your app 'doesn't care' so to speak -- as far as it is concerned, it's never disappeared and it's still visible ...

What triggers viewWillAppear?

The method viewWillAppear: is triggered in response to a change in the state of the application, indicating that the view controller is becoming “active.” The reason viewDidLoad exists – the only reason – is that it sometimes isn't possible or efficient to configure 100% of an interface in a XIB.

How many times is viewWillAppear called?

If both these methods are called, what's the difference between the two? viewDidLoad() is only called once, when the view is loaded from a . storyboard file. viewWillAppear(_:) is called every time the view appears.

Does viewDidLoad get called before viewWillAppear?

viewWillAppear(_:)Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called.


2 Answers

You need to set the correct presentationStyle. If you want that your presentedController will be fullScreen and call it the previous viewWillAppear, then you can use ".fullScreen"

let viewController = self.storyboard?.instantiateViewController(withIdentifier: "LoginController") as! LoginController  let navigationController: UINavigationController = UINavigationController(rootViewController: viewController)  navigationController.modalPresentationStyle = .fullScreen  present(navigationController, animated: true, completion: nil) 
like image 173
Kevinosaurio Avatar answered Sep 27 '22 18:09

Kevinosaurio


In iOS 13 , if you are presenting a view controller and when you are coming back viewWillAppear doesn't get called . I have changed it from present to push view controller and methods are getting called now .

like image 24
Sateesh Pasala Avatar answered Sep 27 '22 20:09

Sateesh Pasala