How do I pass the data from PFTableViewCell to the next ViewController using prepareForSegue in swift 2.0?

149 Views Asked by At

I am trying to pass data from a PFTableViewCell to my next view controller(details) but I am still not able to see the data. I think wrote my code correctly for Swift 2.0/Xcode 7. I've been stuck on this for about 2 weeks and I'm not good with coding at all. Is there any other code that could pass the data to other viewcontroller?

In my FirstViewController I have this code written:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)  {
        if segue.identifier == "Detail" {
            if let destination = segue.destinationViewController as? DetailsViewController
            , path = tableView?.indexPathForSelectedRow
            , cell = tableView?.cellForRowAtIndexPath( path ) as? Post
        {
            destination.name = cell.name.text ?? "nil"
            destination.message = cell.message.text ?? "nil"
        }

and in my DetailsViewController I have this written:

    var name: String?
var message: String?

@IBOutlet weak var userName: UILabel?
@IBOutlet weak var userMessage: UILabel?

override func viewDidLoad() 
{
    super.viewDidLoad()
    userName?.text = name ?? "nil"
    userMessage?.text = message ?? "nil"
}
1

There are 1 best solutions below

7
On BEST ANSWER

ok if you are using a PFQueryTableViewController, try this:

1) create a manual segue from the PFQueryTableViewController (NOT from the cell, from the view controller) to the view controller you want to transfer the data to. In the inspector panel in the interface builder, name it "yourManualSegue"

2) at the top of your class (pf the PFQueryTableViewController), create 2 variables:

  var nameToPass = String()
  var messageToPass = String()

3) call the didSelectRowAtIndexPath method, like so:

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
       let object = objectAtIndexPath(indexPath)
       self.nameToPass = object!.objectForKey("keyThatholdsNameHere") as! String
       self.messageToPass = object!.objectForKey("keyForMessageHere") as! String

  self.performSegueWithIdentifier("yourManualSegue", sender: self)


}

4) Now, in your prepareForSegue:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)  {
if segue.identifier == "Detail" {
     let destination = segue.destinationViewController as? DetailsViewController
    destination.name = self.nameToPass
    destination.message = self.messageToPass

   }