AlexR
AlexR

Reputation: 5644

Why is self.splitViewController == nil?

I have created a new iOS 8 project using Xcode 6's Master Detail Application template. I have not changed the code in any way.

When setting a breakpoint in DetailViewController.m and inspecting the self.splitViewController property as shown in the following screenshot, it returns nil.

enter image description here

Why is that?

According to the Apple UISplitViewController Documentation, self.splitViewController should return the nearest SplitViewController:

If the receiver or one of its ancestors is a child of a split view controller, this property contains the owning split view controller. This property is nil if the view controller is not embedded inside a split view controller.

Upvotes: 2

Views: 1199

Answers (2)

malhal
malhal

Reputation: 30727

The setDetailItem in the template is missing a crucial check of isViewLoaded:

- (void)setDetailItem:(Event *)detailItem {
    if (_detailItem == detailItem) {
        return;
    }
    _detailItem = detailItem;

    if(self.isViewLoaded){
        // Update the view.
        [self configureView];
    }
}

This allows setting of the detail before the view is loaded (like in prepareForSegue) when the things you require would otherwise be nil and also prevents it accidentally loading the view, if you were using self.view inside configureView for something.

Upvotes: 1

Rory McKinnel
Rory McKinnel

Reputation: 8014

Don't know if you ever figured this out, but I just had this issue and found what was wrong and looks similar to your case.

The splitViewController property does not get configured until after viewDidLoad. If by accident your code using it gets triggered before viewDidLoad then the value will be nil.

I notice in your code example that configureView is called from setDetailItem. If that is per chance being called from prepareForSegue then viewDidLoad will not have happened yet resulting in a nil for splitViewController property value inside configureView. So code has to be run after after [super viewDidLoad] has completed.

Upvotes: 8

Related Questions