0

I have a view controller with a toolbar with 3 UIButtons that open a new view controller as a popover. I created the segues in Storyboard and selected "Present as Popover". The popovers work but when the user taps on another button while a popover is currently open, I get this error:

Warning: Attempt to present <Fingerpainter.OpacityViewController: 0x79095110>  on <Fingerpainter.DrawingViewController: 0x7b278000> which is already presenting <Fingerpainter.BrushSizeViewController: 0x79573770>

Is there a way to like make sure all popovers are closed before opening a new one? Here's my prepareForSegue method in the main ViewController (containing the toolbar):

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let identifier = segue.identifier ?? ""
    let popoverPresentationController = segue.destinationViewController.popoverPresentationController
    popoverPresentationController!.delegate = self
switch identifier {
    case Storyboard.BrushSizeSegueIdentifier:
        if let brushSizeViewController = popoverPresentationController?.presentedViewController as? BrushSizeViewController {

            // set properties in brushSizeViewController
        }
    case Storyboard.OpacitySegueIdentifier:
        if let opacityViewController = popoverPresentationController?.presentedViewController as? OpacityViewController {
            //set properties in opacityViewController
        }
    case Storyboard.ColorSegueIdentity:
        if let colorViewController = popoverPresentationController?.presentedViewController as? ColorViewController {
            //set properties in colorViewController
        }
    default:
        break
    }

}

Morgan
  • 1,280
  • 1
  • 14
  • 15

1 Answers1

0

Is there a way to like make sure all popovers are closed before opening a new one

It's the other way around. It's your job to make sure that while the popover is present, a button that summons another popover is not tappable. You can do this by disabling the button, but more commonly, in order to coordinate the disabling of the button with the presence of the popover, it's done by adjusting the popover presentation controller's passthroughViews.

Unfortunately there's a massive and long-standing bug where even setting the passthroughViews to nil doesn't prevent toolbar buttons from being tappable. The workaround is to do it with a delay. A lot of my popover code adds this sort of thing:

if let pop = popoverPresentationController {
    delay(0.1) {
        pop.passthroughViews = nil
    }
}

(where delay is described here: https://stackoverflow.com/a/24318861/341994).

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141