1

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/

  • It's not clear what you mean by "fit that statement above in this". If your segue is connected to a cell, then the sender argument (in prepareForSegue:sender) will be the cell, and you can use the table view method, indexPathForCell: to get the indexPath. You don't need to implement didSelectRowAtIndexPath at all. – rdelmar Dec 24 '14 at 00:40
  • How do connect a segue to a cell click? – James Shelton Dec 24 '14 at 01:26
  • Right click (or control click) on the cell, and drag to the controller (the same as you do for any segue). – rdelmar Dec 24 '14 at 01:33
  • Is it possible to recognize a sharedinstance value defined in one class, in another class, or view controller? – James Shelton Dec 24 '14 at 01:50
  • I don't know what you men by "shared instance value". In any case, you can't make IBOutlet connections between controllers. – rdelmar Dec 24 '14 at 03:43
  • Are you telling me I can not take the value of what was displayed in a table view cell through cell.textLabel!.text can not be passed into another view controller label or text field? – James Shelton Dec 24 '14 at 03:49
  • println (too) outputs when cell is clicked value of let too: AnyObject = rowData ["time"] as NSString ..... how can I get that same value in the next view controller that appears? – James Shelton Dec 24 '14 at 03:52
  • No, that's not what I was saying. You can do that, and there are hundreds of answers on SO about passing data between controllers, so you should do some searching. – rdelmar Dec 24 '14 at 03:58
  • I have only found two, singletons, and prepare for segues. But I am not able to combine a prepare for segue with the tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {} – James Shelton Dec 24 '14 at 04:05
  • This search, "[ios] pass data between view controllers" turned up 667 results. Also, as I told you in my first comment, you shouldn't implement didSelectRowAtIndexPath at all in this situation, only prepareForSegue. – rdelmar Dec 24 '14 at 04:09
  • I need didSelectRowAtIndexPath to push segue and set off alert, and run an NSDictionary. – James Shelton Dec 24 '14 at 04:35
  • How can prepareforsegue recognize what cell was clicked without referencing the viewtable? – James Shelton Dec 24 '14 at 04:36
  • You can do all of the same code in prepareForSegue. As I said, the sender will be the cell, so you can use NSIndexPath *path = [self.tableView indexPathForCell:(UITableViewCell *)sender]; Once you have the indexPath, you can access your model with it to get any data you need. – rdelmar Dec 24 '14 at 04:42

1 Answers1

0

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.

Blake Morgan
  • 767
  • 1
  • 7
  • 25
  • Sure thing! Please up-vote the answer too if you thought it was good. – Blake Morgan Dec 24 '14 at 06:31
  • I tried up voting you, I don't have 15 creds so I can't upgrade. If you think my question was good, please up vote it! – James Shelton Dec 24 '14 at 06:41
  • I forgot to add this: The final statement you should make after editing the defaults in a method is the `defaults.synchronize()` function. That will save all your change to the disk. – Blake Morgan Dec 24 '14 at 22:10