viewWillDisappear can be called before viewDidLoad violating view lifecycle expectations and causing crashes.

Originator:crayment16
Number:rdar://35972275 Date Originated:11-Dec-2017 12:06 PM
Status:Open Resolved:
Product:iOS + SDK Product Version:iOS 11.2 (15C107)
Classification:Serious Bug Reproducible:Always
 
Summary:
It is possible to set up a situation where viewWIllDisappear is called before any of viewDidLoad, viewWillAppear, and viewDidAppear when programmatically switching tabs in UITabBarController.

Steps to Reproduce:
Our situation involves a UITabBarController with logic in its viewDidLoad to call setSelectedViewController. We also have state restoration that restores tab selection. When both of these fire and select different tabs we get a situation where viewWillDisappear is called on one of our tabs before its view has been loaded. When we then access an implicitly unwrapped optional IBOutlet in viewWillDisappear we crash.

Expected Results:
The view to always be loaded before a viewWillDisappear, viewDidDisappear, viewWillAppear, viewDidAppear, events can be called. I have never seen any Apple sample code guarding to ensure the view is loaded before accessing IBOutlets in any of these lifecycle hooks.

Also, viewWillDisappear should never be called without viewWillAppear being called at some earlier point. This seems to be a direct violation of the view lifecycle documentation.

Actual Results:
- viewWillDisappear is called before viewDidLoad.
- viewIsLoaded returns false.
- viewWillAppear, and viewDidAppear are not called before viewWillDisappear, violating lifecycle expectations.

Version:
iOS 11.2 (15C107)

Notes:
To workaround we have used: `guard isViewLoaded else { return }` before accessing any IBOutlets in `viewWillDisappear`.

Comments

Note: Shortly after viewWillDisappear is called with isViewLoaded=false viewDidDisappear is called with isViewLoaded=true.

By crayment16 at Dec. 11, 2017, 7:16 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!