0

I am trying to implement UIPopoverPresentationController in my app to display tableViewController. It works fine when app runs on an iPhone but crashes on an iPad.

The code that is the problem is:

    - (void)soundsButtonHandler:(UIBarButtonItem *)barButtonItem {

    IESoundsTableViewController *soundsTVC = (IESoundsTableViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"SoundsTableViewController"];

    soundsTVC.baseSceneViewController = self;

    soundsTVC.popoverPresentationController = [[UIPopoverPresentationController alloc] initWithPresentedViewController:soundsTVC presentingViewController:self];


      soundsTVC.modalPresentationStyle = UIModalPresentationPopover;

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



    self.popoverPresentationController = soundsTVC.popoverPresentationController;

    self.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;

    self.popoverPresentationController.barButtonItem = barButtonItem;
}

This works just fine on an iPhone & brings up the tableView modally with the usual vertical presentation.

However, it fails on an iPad with the following stack trace:

- objc_exception_throw ()
- -[UIPopoverPresentationController presentationTransitionWillBegin] ()
- __71-[UIPresentationController _initViewHierarchyForPresentationSuperview:]_block_invoke ()
- __56-[UIPresentationController runTransitionForCurrentState]_block_invoke ()
- _applyBlockToCFArrayCopiedToStack ()
- _afterCACommitHandler ()
- __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
- __CFRunLoopDoObservers ()
- __CFRunLoopRun ()
- CFRunLoopRunSpecific ()
- GSEventRunModal ()
- UIApplicationMain ()

I've tried several variations of code ordering & using a sourceView & sourceRect instead of barButtonItem for the popover anchor, but none of them help.

Thanks.

cyberlobe
  • 1,783
  • 1
  • 18
  • 30
Paul Linsay
  • 449
  • 4
  • 6
  • Possible duplicate of [ActionSheet not working iPad](https://stackoverflow.com/questions/28089898/actionsheet-not-working-ipad) – Tieme Jul 04 '19 at 09:33

1 Answers1

0

You have to use below code for this..

   if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        [self presentViewController:activityViewController animated:YES completion:nil];
    }
    //if iPad
    else {
        // Change Rect to position Popover
        UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
        [popup presentPopoverFromRect:yourRectWhatever  inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
    }
Dipen Chudasama
  • 3,063
  • 21
  • 42
  • This is how it used to be done and I used to do it. WWDC 2014, session 228 claims that UIPopoverPresentationController does the right thing for both iPhones and iPads without checking for the device type. – Paul Linsay May 15 '15 at 13:39