How to get a variable value from popover made in storyboard? (in Swift)

2.1k views Asked by At

Here is the situation:

I have a Master-Detail setup for my app. In the MasterViewController I have a button called "Filter".

Through the storyboard I set up a popover view that appears once the Filter button is pressed. There is no IBAction on the filter button, or any programmatic way that calls the popover.

In my FilterViewController class I have all the UI elements that populate the popover view, with which the user can interact and select all sorts of stuff.

At the top of the popover view there is an "Update" button.

I want to record the user's decisions, and pass them back to the MasterViewController once "Update" is pressed.

Here is the code:

MasterViewController.swift

class MasterViewController: UITableViewController{

    override func awakeFromNib() {
        super.awakeFromNib()
        self.clearsSelectionOnViewWillAppear = false
        self.preferredContentSize = CGSize(width: 320.0, height: 600.0)
    }

    override func viewDidLoad() {
        super.viewDidLoad()


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
//more standard MasterView code...

}

FilterViewController.swift

class FilterViewController: UIViewController{


var someUIStuff:String?
@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {

    super.viewDidLoad()

    scrollView = self.view as UIScrollView
    scrollView.contentSize = CGSizeMake(300, 600)

}

//presumably, this is from where I should pass the variables to the MasterView

@IBAction func updateBtnPressed(sender: AnyObject) {

//this actually gets set by user in the popover view; I have no problem recording it, but I want to pass *someUIStuff* back to MasterView

someUIStuff = "Hello, MasterView" 

 }

//other code that populates the popover with UI stuff

relevant part of storyboard:

enter image description here

The arrow between the two views is a Popover presentation segue to Filter View Controller

I have tried following the instructions here:

SWIFT: No idea how to get back the selected value from a popover to the calling controller

Swift, Pass data back from popover to view controller

To no avail; both seem to use some programmatic access to the popover view which I do not have.

Any help is greatly appreciated!

Best,

Jona

1

There are 1 answers

7
Bishan On

If you want to pass data from one view to another you can simply set NSUserDefaults and read it wherever you need.

Think you have variable call Name in view No.01, You can store Name in NSUserdefault object by giving it a unique key(id). In this case its "name"

NSUserDefaults.standardUserDefaults().setObject(Name, forKey: "name") 

Then think you want to access it in any other view so use the key you assigned and get the value into a variable like this

var getName:String = NSUserDefaults.standardUserDefaults().objectForKey("name") as! String!

Using this you can pass data through view controllers.

Example, Think this is your popup view

class Popupview: UIViewController {

//assume you have two text fields

@IBOutlet var Name: UITextField!

@IBOutlet var Age: UITextField!



override func viewDidLoad() {

}



@IBAction func DoSomething(sender: AnyObject){

//do your stuffs in button action



//here you set the Name into NSUserDefault with key "name"

NSUserDefaults.standardUserDefaults().setObject(Name.text, forKey: "name") 
//here you set the Name into NSUserDefault with key “age”

NSUserDefaults.standardUserDefaults().setObject(Age.text, forKey: “age") 


}

Now you have set Name and Age from your pop up view to NSUserdefaults so now you have to get them into your master view

In your viewDidLoad() method in masterview

override func viewDidLoad() {

var getName:String = NSUserDefaults.standardUserDefaults().objectForKey("name") as! String!
var getAge:String = NSUserDefaults.standardUserDefaults().objectForKey(“age") as! String!
}

Now you have the date in two variables so you can use them anywhere.