iOS: how to dismiss popovers [duplicate]

半腔热情 提交于 2019-12-01 09:21:21

Every time a Segue is performed it creates a new viewController, as you are seeing when you wired your button up to perform the segue. So instead you need to create a "generic" segue for the viewController by dragging from the viewController icon at the bottom to the destination. Then as LucasTizma suggested hook the button up to an IBAction that either calls performSegueWithIdentifier: or dismisses the popover, conditional on whether the popover is on screen or not.

I then do this inside the IBAction:

- (IBAction)settingsButtonTapped:(id)sender {
        [self performSegueWithIdentifier:@"settingsPopover" sender:self];
}

But this gives me this error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIStoryboardPopoverSegue must be presented from a bar button item or a view.'

Have I created the segue in a bad way or in the call to performSegueWithIdentifier wrong?

You're passing self instead of sender (the tapped button) when you call -performSegueWithIdentifier:sender: i.e., you're giving the segue a UIViewController where it expects a UIBarButtonItem or UIView.

This should work:

 - (IBAction)settingsButtonTapped:(id)sender {
         [self performSegueWithIdentifier:@"settingsPopover" sender:sender];
 }

Storyboards are meant to be a general app flow and interface tool. They abstract a lot of the common boilerplate away from your code so you can easily see what's going on a high-level.

Unfortunately, you aren't going to be able to capture the logic you want purely in Storyboards. Just as Storyboards will gladly present a view controller modally as a segue but has no awareness of dismissing a view controller modally, Storyboards will present popover controllers, but it doesn't offer you the capability to dismiss them.

  1. Instead of using a segue for this, just create a custom IBAction for the bar button. Keep track of whether or not the popover is presented and dismiss it in this method if so.

  2. UIPopoverController has a property called popoverContentSize that lets you control this.

Symmetric

A different solution is here. The segue changes the button action to either display or dismiss the popup.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!