Those who claim that this doesn't work are mistaken:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if self.isMovingFromParent {
print("we are being popped")
}
}
That works fine. So what is causing the widespread myth that it doesn’t?
The problem seems to be due to an incorrect implementation of a different method, namely that the implementation of willMove(toParent:)
forgot to call super
.
If you implement willMove(toParent:)
without calling super
, then self.isMovingFromParent
will be false
and the use of viewWillDisappear
will appear to fail. It didn't fail; you broke it.
NOTE: The real problem is usually the second view controller detecting that the first view controller was popped. Please see also the more general discussion here: Unified UIViewController "became frontmost" detection?
EDIT A comment suggests that this should be viewDidDisappear
rather than viewWillDisappear
.