1

I have a popover created in storyboard that simply displays a list that I select from. There are multiple buttons on my view that I need to pick lists for but currently it looks like I have to create the exact same popover for each button.

Does anyone know of a way to use the same popover in storyboard?

Travis M.
  • 10,930
  • 1
  • 56
  • 72
  • Do you mean you created the popover's content view controller in the storyboard? Do you want to use the same instance of the controller very time you present the popover, or just reuse that class? – rdelmar Jun 27 '14 at 20:01
  • Yes, I want to use the same instance and class from multiple buttons, but seems like I can only link it to one. For example, if I made a popover in STORYBOARD that simply has a label and says "Hello" and want to launch it from 2 buttons on the ViewController.. is that possible? – Travis M. Jul 02 '14 at 17:00

2 Answers2

4

With Storyboards (and the new and improved NSViewController) it's very easy to create popover segues from many different sources. This image shows the simplest of examples; a popover with a text field that changes depending on which button was pressed:

Storyboard with App running on top

The View Controller for the Popover was subclassed to add a "name" variable, but you can add whatever object(s) you wish to bind to in the view.

The main View Controller implements the prepareForSegue function. Depending on which object was the sender, it sets the PopoverController's "name" property to different values.

Here's all the code that is needed make this example work:

//  ViewController.swift

import Cocoa

class PopoverController: NSViewController { var name: String? }

class ViewController: NSViewController {

override func prepareForSegue(segue: NSStoryboardSegue!, sender: AnyObject!) {
    if let popoverController: PopoverController? = segue.destinationController as? PopoverController! {
        if let button: NSButton? = sender as? NSButton! { switch (button!.title!) {
            case "One": popoverController!.name = "First Button"
            case "Two": popoverController!.name = "Second Button"
            default: true } }
    }
}}
ElmerCat
  • 3,126
  • 1
  • 26
  • 34
0

If you want to present the same popover from different buttons, then you should instantiate the content controller and popover controller in code rather than using a segue, because segues always instantiate new controllers. Something like this should work (tableVC and popController are both strong properties),

- (IBAction)showPopover:(UIButton *)sender {
    if (! self.tableVC) {
        self.tableVC = [self.storyboard instantiateViewControllerWithIdentifier:@"Table"];
        self.popController = [[UIPopoverController alloc] initWithContentViewController:self.tableVC];
        self.popController.delegate = self;
    }

    [self.popController presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218