Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace the deprecated method splitViewController:willHideViewController:withBarButtonItem:forPopoverController:

My standard implementation for this delegate method is the following. I just initialize the navigation button and save locally the button and the popover.

- (void) splitViewController: (UISplitViewController *) splitController
      willHideViewController: (UIViewController *)      viewController
           withBarButtonItem: (UIBarButtonItem *)       barButtonItem
        forPopoverController: (UIPopoverController *)   popoverController
{
    // Set the button to open the PopOver
    barButtonItem.title = viewController.title;
    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];

    // Save the ref to the default left navigation button
    _masterButton = barButtonItem;

    // Save the ref to the PopOver
    _masterPopOver = popoverController;
}

From iOS 8 this method is deprecated and the Apple documentation says:

Implement the splitViewController:willChangeToDisplayMode: method instead.

But the arguments of the new method has nothing to do with the deprecated method! I guess I have to create a button and a popover myself?

Does somebody already made this re-coding to implement the current popup behaviour?

Thank you for your help

like image 475
PatrickV Avatar asked Oct 07 '14 01:10

PatrickV


2 Answers

Take a look at displayModeButtonItem. It is very similar to barButtonItem from the deprecated method.

You can refactor your example into using the new splitViewController:willChangeToDisplayMode: method in the following way:

- (void)splitViewController:(UISplitViewController *)svc
    willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode {

    if (displayMode == UISplitViewControllerDisplayModePrimaryHidden) {
        self.navigationItem.leftBarButtonItem = svc.displayModeButtonItem;
    }
}
like image 109
Alex Tamoykin Avatar answered Oct 26 '22 19:10

Alex Tamoykin


This is an extension to Alexander's answer. To cover Cihad's comment: the last line of code creates the leftBarButtonItem and makes it the blue "<" button that will open the master viewController.

I just commented out willHideViewController and willShowViewController from my detail viewController and cut and pasted Alexander's code. Worked first time.

Then I discovered that if I started the app in portrait it did not work until I went landscape and back to portrait. Obviously the method is not called until a change of orientation.

So I added this code in my viewDidLoad method of my detail viewController and it worked fine:

        //Set up the splitview controller
    if (self.splitViewController.displayMode == UISplitViewControllerDisplayModePrimaryHidden) {
        self.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;}

splitViewController is a property of your detail viewController that should be there for you to use.

like image 28
Tim Avatar answered Oct 26 '22 20:10

Tim