husharoonie
husharoonie

Reputation: 441

Swift 4 Call ViewWillAppear after dismissing View Controller

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.

Upvotes: 3

Views: 16241

Answers (3)

user7396942
user7396942

Reputation:

yout modal should be .fullScreen

Try with this:

let createAccounts = CreateAccounts();
        let navController = UINavigationController(rootViewController: createAccounts)
        navController.modalPresentationStyle = .fullScreen

Upvotes: 1

Kowboj
Kowboj

Reputation: 371

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

Upvotes: 6

Tushar Katyal
Tushar Katyal

Reputation: 422

// 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

Upvotes: 1

Related Questions