UISplitViewController calling viewWillDisappear(_:) before the view is loaded at launch on iOS 11.2

382 Views Asked by At

I have an app with a UISplitViewController as the initial controller, and it has both a master and a detail view controller (embedded in UINavigationControllers) as its child view controllers, laid out in Interface Builder. The split view controller's preferredDisplayMode is set to .primaryHidden.

I just upgraded to Xcode 9.2, which uses iOS 11.2 in the simulator. When I launch the app on an iPhone, either a device or simulator, I see a new behavior at launch: viewWillDisappear(_:) is called BEFORE viewDidLoad() on the detail view controller. At this point the detail view controller is not yet loaded - all of its IBOutlets are nil. After that call, the system loads the view and calls viewDidLoad() as usual.

I discovered this because that viewWillDisappear(_:) method accessed a UITextField to check if it was the first responder. In the code below, textfield is connected to a UITextField via IBOutlet:

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

    if textfield.isFirstResponder {
        textfield.resignFirstResponder()
    }
}

Since the view wasn't yet loaded, textfield was nil and the app crashed. This was easy to fix by adding a test for nil. But in earlier versions of Xcode and iOS, the crash didn't occur because, I assume, viewWillDisappear(_:) was not called before the view was loaded.

Also interestingly, this doesn't occur with Xcode 9.2 running iOS 11.2 on an iPad, either device or simulator. On an iPad, viewWillDisappear(_:) is not called at launch on the detail view controller.

This seems like a bug: why should viewWillDisappear(_:) be called on the detail view controller (or on any view controller) before the view is actually loaded at launch. Is anyone else is seeing this behavior with UISplitViewController on an iPhone running iOS 11.2? Or is it the consequence of something I've overlooked, even though the app worked without issues in earlier versions?

0

There are 0 best solutions below