Sachintha Udara
Sachintha Udara

Reputation: 701

View Controller dismissing and reappearing previous view Controller

I am new to iOS development and Swift, I have issue with view controllers events, what is the event automatically should be called when the top view controller dismisses and previous view controller shown again?

Actually what I mean is I have view controller A and view controller B, I click one button (assume go B view controller) of view controller A and move for view controller B then doing some task of view controller B and click one button (assume go back) in that time I dismiss view controller B then application automatically show view controller A, In this situation I have to fire one method, but my problem is view controller A how is it knows that it will reappear to be displayed?

Upvotes: 1

Views: 1546

Answers (1)

Ahmad F
Ahmad F

Reputation: 31645

Based on your case,

View Controller A knows that it reappears by implementing one of the UIViewController tow methods -depends on your requirement(s)-:

Notifies the view controller that its view is about to be added to a view hierarchy.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // ...
}

Or

Notifies the view controller that its view was added to a view hierarchy.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // ...
}

Note that both of these methods will also be executed when the view controller has been presented/pushed, not only after being dismissed/popped.

In your case, you would need to know if View Controller B is disappearing or has disappeared. For this purpose, you could also implement one of the UIViewController methods -also -depends on your requirement(s)-:

Notifies the view controller that its view is about to be removed from a view hierarchy.

Or

Notifies the view controller that its view was removed from a view hierarchy.


Thus, assuming that the current presented view controller is view controller B and it will be dismissed, the hierarchy of combining the call of the methods between the two view controllers should be as follows:

View Controller B => viewWillDisappear(_:).

View Controller A => viewWillAppear(_:).

View Controller B => viewDidDisappear(_:).

View Controller A => viewDidAppear(_:).

Upvotes: 1

Related Questions