简体   繁体   中英

Reuse Table View variables Swift

Here is the table view:

   public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        var rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
           let too: AnyObject = rowData ["time"] as NSString
        var name: String = rowData["time"] as String
        var formattedPrice: String = rowData["date"] as String

        var alert: UIAlertView = UIAlertView()
        alert.title = name
        alert.message = formattedPrice
        alert.addButtonWithTitle("Ok")
        alert.show()
         println ("hi")
         println (too)   
    }

I need to reference these variables in another view controller. I have not been able to fit that statement above in this:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "segueTest") {
var svc = segue!.destinationViewController as secondViewController;
svc.toPass = textField.text
}
}

I am trying to segue off cell click. from http://jamesleist.com/ios-swift-passing-data-between-viewcontrollers/

If it isn't a lot of data, the strategy I use to pass data between view controllers is to store the value in NSUserDefaults .

Setting A Value : When you first get the data, store it in NSUserDefaults .

let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults() //This class variable needs to be defined every class where you set or fetch values from NSUserDefaults
public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
    let too: AnyObject = rowData ["time"] as NSString
    var name: String = rowData["time"] as String
    var formattedPrice: String = rowData["date"] as String
    defaults.setObject(rowData, forKey: "rowData")
    defaults.setObject(too, forKey: "too")
    defaults.setObject(name, forKey: "name")
    defaults.setObject(formattedPrice, forKey: "formattedPrice")
    defaults.synchronize() //Call this after you're done editing defaults, it saves your change to the disk

    var alert: UIAlertView = UIAlertView()
    alert.title = name
    alert.message = formattedPrice
    alert.addButtonWithTitle("Ok")
    alert.show()
     println ("hi")
     println (too)   
}

Fetching A Value : When you need to get the values, just grab it from NSUserDefaults .

let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.dictionaryForKey("rowData") as? NSDictionary
defaults.objectForKey("too") as? String
defaults.objectForKey("name") as? String
defaults.objectForKey("formattedPrice") as? String

Doing that gives you access to the stored values in any class and allows the data to persist after the app is closed and reopened. If you want to clear the data after the app closes, in AppDelegate applicationWillTerminate(application: UIApplication) function, call the removeObjectForKey function for each key previously set.

let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.removeObjectForKey("rowData")
defaults.removeObjectForKey("too")
defaults.removeObjectForKey("name")
defaults.removeObjectForKey("formattedPrice")
defaults.synchronize()

Helpful Source on NSUserDefaults :

NSUserDefulats Class Reference: Link here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM