0

I'm a beginner. I am doing a popover when a button is pressed which then instantiates another view controller where the user can select from 5 choices. I want to be able to save the sender.tag of the button from the first view controller (where code snippet below came from) and pass it to the second where I can save them together to Parse. I'm not using a segue so I can't pass it that way. Thanks in advance!

func showPopover(sender: UIButton) {

      let vc = self.storyboard?.instantiateViewControllerWithIdentifier("SelectionViewController")
      vc!.modalPresentationStyle = .Popover
      vc!.preferredContentSize = CGSizeMake(150, 30)

      if let presentationController = vc!.popoverPresentationController {
         presentationController.delegate = self
         presentationController.permittedArrowDirections = .Up
         presentationController.sourceView = self.view
         presentationController.sourceRect = sender.frame

         self.presentViewController(vc!, animated: true, completion: nil)
      }
   }
tahoecoop
  • 378
  • 2
  • 9
  • 30
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – jtbandes Sep 30 '15 at 02:20

3 Answers3

0

The easiest way would be to declare a variable var myVariable = Int() outside of either view controller class. Then, inside of your main VC and before you instantiate the popover, save the tag to the variable. You'll be able to use it inside the popover.

Naftali Beder
  • 1,066
  • 1
  • 10
  • 27
0

You could just use a segue (why aren't you?)

You put a property for the tag in the popover and set it in the first view controller, inside

func prepareForSegue(_ segue: NSStoryboardSegue, sender sender: AnyObject?).

To perform the segue, you just use

func performSegueWithIdentifier(_ identifier: String, sender sender: AnyObject?)

, inside your function

func showPopover(sender: UIButton)

Ivens Denner
  • 523
  • 5
  • 12
0

If you don't want to use a segue you can simply cast the controller you get from instantiateViewControllerWithIdentifier() to your subclass.

Swift 2.0

 func showPopover(sender: UIButton) {
    guard let vc = self.storyboard?.instantiateViewControllerWithIdentifier("SelectionViewController") as? MYViewController
    } else {
        print("This is not the view controller you were looking for..."
        return
    }

    vc.myVariableName = sender.tag

    ... 
 }