UIPopoverPresentationController should have a non-nil sourceView or barButtonItem set before the presentation occurs on iOS 9 UIPopoverPresentationController should have a non-nil sourceView or barButtonItem set before the presentation occurs on iOS 9 objective-c objective-c

UIPopoverPresentationController should have a non-nil sourceView or barButtonItem set before the presentation occurs on iOS 9


You can create a popover presentation controller like this also and it may work

- (IBAction)showPopup:(UIButton *)sender {ViewController *contentViewController = [[ViewController alloc] init];    contentViewController.preferredContentSize = CGSizeMake(200, 200);    contentViewController.modalPresentationStyle = UIModalPresentationPopover;UIPopoverPresentationController *popoverpresentationController = contentViewController.popoverPresentationController;    popoverpresentationController.delegate = self;    popoverpresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;    popoverpresentationController.sourceRect = sender.bounds;    popoverpresentationController.sourceView = sender;    [self presentViewController:contentViewController animated: YES completion: nil];}


You are subclassing the UIPopoverPresentationController but Apple recommends to use them as they are. Once you present a UIViewController, a UIPopoverPresentationController will be automatically created and you are supposed to modify it for your needs.

You create an myPopoverController instance but Apple creates another when you present your contentViewController right after:

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

This new UIPopoverPresentationController lacks the sourceView and throws an exception.

Try the code below instead:

ViewController *contentViewController = [[ViewController alloc] init];// Present the view controller using the popover style.contentViewController.modalPresentationStyle = UIModalPresentationPopover;[self presentViewController:contentViewController                    animated:YES                  completion:nil];// Get the popover presentation controller and configure it.UIPopoverPresentationController *presentationController =[contentViewController popoverPresentationController];presentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;presentationController.sourceView = self.view;presentationController.sourceRect = sender.frame;


If sourceView is null, just add a validation

UIActivityViewController * avc = [[UIActivityViewController alloc] initWithActivityItems:shareItems applicationActivities:nil];if(avc.popoverPresentationController){    avc.popoverPresentationController.sourceView = self.view;}[self presentViewController:avc animated:YES completion:nil];

See this post