Search code examples
swiftuitableviewdelegatesuipopovercontroller

Passing Data with Delegates


Im having a hard time understanding data passing with delegates, I have looked but I get confused by the answers as I'm usually trying to do the opposite of what needs to be done for mine.

What I am trying to do is pass a PFObject from my main viewController (a UITableView) to a PopOverViewController.

I have done this successfully, but I need to pass the object. What would be the best way to pass from the mainController (SOITableViewController) to the popover (DetailPopViewController)?

Where should the protocol go? Where should the Delegate method be placed, etc.

Thank you!


Solution

  • PrepareForSegue, NSUserDefault and Singleton

    You have a few possible options to pass your data to other views depending how you want that data to be handled, I will explain each for you and you can choose which one best fit your need.


    prepareForSegue: Method

    I recommend this method if you want to hold your data for 1 segue transition, it's a good cause to pass this again to another view afterward you need to create another prepareForSegue within the new view. here is an example on how to do this:

    First, you create 2 variables in both views, 1 to send (currentViewController.swift) and 1 to receive (toViewyourGoingController.swift).

    currentViewController.swift var dataToSend: AnyObject?
    ViewYourGoingController.swift var dataToReceive: AnyObject?

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
        //Check your segue, this way you can transfer different data to different view. also make sure the identifier match your segue.
        if segue.identifier == "toViewYourGoing" {
    
            //Initial your second view data control
            let ExchangeViewData = segue.destinationViewController as! toViewyourGoingController
            
            //Send your data with segue 
            ExchangeViewData.dataToReceive = dataToSend
        }
    }
    

    NSUserDefault

    Now this method is good if you want to keep your data live as long as the app is installed, once the app is removed this will reset automatically. You also have the option to update the value of the key if you wish, here is how you do NSUserDefault:

    I always like to register my NSUserDeafult to default setting, a lot of people just continue with the second step without registering.

    Register NSUserDefault in AppDelgate.swift

    NSUserDefaults.standardUserDefaults().registerDefaults(["valueName": AnyObject])
    

    Set Value to your NSUserDefault, this depends on what type of data you're storing, should match the one with your registration if you did register. (Example of Boolean data type below)

    NSUserDefaults.standardUserDefaults().setBool(true, forKey: "valueName") //Bool Data Type
    

    Make sure you synchronize once you set the value to the NSUserDefault, this way it will update instantly, otherwise it will update when it get a chance.

    NSUserDefaults.standardUserDefaults().synchronize()
    

    Receive Value: this will receive boolean value since we set boolean and register boolean.

    let Variable: Bool! = NSUserDefaults.standardUserDefaults().boolForKey("valueName")
    

    Singleton

    Now singleton is basically a global variable that you can use them in any views, but some developers experience some bugs and difficulties, use it at your own risk, I recommend this method when you're definite that you will use that data a lot (STILL RISKY), but this method is like goddess of data handling :).

    Create a NSObject subclass and call it DataManager.swift (I call it data manager cause it handle data.) as following:

    import UIKit

    class DataManager: NSObject {
            
       //Store Data Globally
       static var someData: Boo! //This Boolean, you can choose whatever you want.
    }
    

    the static is what keep your data live.

    Now you can store and receive someData from anywhere like you handle any data type like this.

    //Store
    DataManager.someData = true
    
    //Receive 
    print(DataManager.someData)
    

    Challenges:

    You can also use

    Keychain Sergey Kargopolov will walk you through how to use a third party to use swift keychain. Otherwise, you can take even harder challenge and create one yourself :P .

    Key-Value Data in iCloud