5

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?

jsblau
  • 81
  • 7
  • We also encountered this bug. I created a simple project that demonstrates the bug. https://github.com/paulnicholson/SplitViewTest/ It also doesn't crash on the iPhone Plus if the phone is in landscape mode or if you are simulating an older iOS version. – Paul Nicholson Dec 11 '17 at 22:04
  • I see the same behavior. The bug occurs when the split view controller is collapsed to one container, and an iPhone Plus in landscape mode is "regular" width, so the split view controller opens to two containers. – jsblau Dec 12 '17 at 20:52

0 Answers0