Problem using barButtonItem for popoverPresentationController in iOS 9?

I had some code using the new UIAlertController class that works great in iOS 8. Now it crashes in iOS 9 with the following error message:

2015-07-23 10:38:27.499 MyApp[828:563509] -[UITabBarItem _viewForPresenting]: unrecognized selector sent to instance 0x157644960
2015-07-23 10:38:27.500 MyApp[828:563509] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarItem _viewForPresenting]: unrecognized selector sent to instance 0x157644960'

      

The problem seems to be using the barButtonItem field to customize the popoverPresentationController, rather than just using the sourceView / sourceRect property. If I switch to the latter, that's fine (but of course the action sheet doesn't pop up from where I wanted it to pop up). Currently, the error message for the error message is shown blank.

Here is the code. This is pretty basic, there is nothing interesting here:

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"What do you want to do?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alertController addAction:[UIAlertAction actionWithTitle:@"Clear Call History" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        [self confirmClearCallHistory];
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}]];
    [alertController setModalPresentationStyle:UIModalPresentationPopover];

    //if I do this (like I want to), it crashes:
    [alertController popoverPresentationController].barButtonItem = self.tabBarController.callsTab;

    // if I do this, it fine:
    // [alertController popoverPresentationController].sourceView = self.editButton;
    //  [alertController popoverPresentationController].sourceRect = self.editButton.bounds;

    [self presentViewController:alertController animated:YES completion:nil];

      

Has anyone had similar problems? It's the same in all three beta versions of iOS 9 so far ...

(Oh, and I must mention that it only crashes on iPad, not iPhone ... but this is not surprising, because the presentation mode for the action sheet is non-op on the iPhone i.e. all action sheets are presented the same no matter what)

+3


source to share


4 answers


Oh, interesting. I am actually passing the UITabBarItem instance to the barButtonItem field. (I inherited the old code, so I didn't know about it) Somehow, did this actually work on iOS 8? Hover over your mouse.



So yeah, I can't do it. heh.

+2


source


Here is the approach I am using. I am getting the view value from the UIBarButtonItem and use it as the original view.



 func settingsButtonAction(sender:UIBarButtonItem) {
        let viewController = UIViewController()
        viewController.modalPresentationStyle = .Popover
        if let presenter = viewController.popoverPresentationController {
            let targetView = sender.valueForKey("view") as! UIView
            presenter.sourceView = targetView;
            presenter.sourceRect = targetView.bounds;
        }
        presentViewController(viewController, animated: true, completion: nil)
    }

      

+2


source


In your handler method, if you have the sender as "AnyObject". You can directly apply,

alertController.popoverPresentationController?.sourceRect = sender.bounds
alertController.popoverPresentationController?.sourceView = sender as? UIView
self.presentViewController(alertController, animated: true, completion: nil)

      

+1


source


Oh cool! It looks like you can set the source bar button element to UIPopoverPresentationController.

popoverPresentationController?.barButtonItem = myBarButtonItem

      

I did this and it works great without having to dig through private views.

According to the documentation, this has been available since iOS 8. Thus, it has always been present in iOS 9.

0


source







All Articles