Źmicier Fiedčanka
Źmicier Fiedčanka

Reputation: 43

How could ViewWillDisappear be called without ViewDidDisappear in Swift

I'm currently studying App Development with Swift course and the authors stated, that it's possible to call ViewWillDisappear(:) without a corresponding call of ViewDidDisappear(:), but given no example or explanation.

I am either misunderstanding the purpose of those methods or just having a lack of experience and imagination to come with a good example on my own (I tried to make a print statement after every View LifeCycle event and testing app in that mode, but still found only "paired" statements). Thanks in advance.

Upvotes: 1

Views: 4316

Answers (2)

Duver Arthur
Duver Arthur

Reputation: 98

One of the possibilities I can think of is a specific case:

You have a View named VC1. On top of VC1, you display another View named VC2. VC2 is not full screen, it is a view that you can dismiss by dragging it down. When you begin dragging down VC2 to dismiss it, it will trigger the viewWillDisappear. Now if you drag it back up, the viewDidDisappear will not get called. If you keep on dragging it down though, the View will get destroyed and viewDidDisappear will get called.

Screen capture

Note: If you want to play with the View lifecycle methods, I find this repository quite handy.

Upvotes: 4

SM Abu Taher Asif
SM Abu Taher Asif

Reputation: 2331

ViewWillDisappear, ViewDidDisappear these are delegate methods of UIViewController.

ViewWillDisappear Notifies the view controller that its view is about to be removed from a view hierarchy. link: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621485-viewwilldisappear

viewDidDisappear Notifies the view controller that its view was removed from a view hierarchy. link : https://developer.apple.com/documentation/uikit/uiviewcontroller/1621477-viewdiddisappear

So According to viewController's life cycle ViewWillDisappear will get called before viewDidDisappear. You don't need to call these methods explicitly. These methods will get called autometically when your view will got removed from a view hierarchy.

Lets , you have two viewContreollers named "A" and "B". Currently you are in viewController A. From A when you goto viewController B , the viewController A will be removed from view hierarchy. In viewController A if you implement below methods and set debug point:

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
}

you will find, viewWillDisappear will get called when you about to goto viewController B and viewDidDisappear will get called when you are in viewController B.

According to viewController's life cycle viewWillDisappear must be called before viewDidDisappear
check the link: https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html

Upvotes: 2

Related Questions