0

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"
}
JoshyJay
  • 53
  • 1
  • 6
  • You can't access the properties in `viewDidLoad` because this method is called before `prepareForSegue` executes – Paulw11 Oct 29 '15 at 01:08
  • Well not using 'viewDidLoad' won't pass the data anyway. I still see the static labels when I run my app. – JoshyJay Oct 29 '15 at 01:30
  • Do you see "nil" or whatever you entered in the storyboard? Have you connected the labels in your storyboard to the IBOutlet? – Paulw11 Oct 29 '15 at 01:35
  • Yes the code for the IBOutlets is shown above. and yes nil is in my storyboard. – JoshyJay Oct 29 '15 at 01:44
  • You have added IBOutlets, but are they connected? ie. is `userName` nil? What about if you change the text in your code *or* in your storyboard? Is the "nil" you see coming from your code or the storyboard? – Paulw11 Oct 29 '15 at 02:37
  • Yes they are connected. The nil appearing is coming from the code not the storyboard. – JoshyJay Oct 29 '15 at 02:40
  • So, where are you trying to set the text now? `viewWillAppear`? – Paulw11 Oct 29 '15 at 02:46
  • I don't know but do I put the code from 'viewDidLoad" to 'viewWillAppear'. I just want to see the PFObjects from my PFTableViewCell passed to the next ViewController. I have the labels on my storyboard and they're connected to my viewcontroller. – JoshyJay Oct 29 '15 at 03:06
  • Try moving the code there, or even better create setter functions for your two properties and update the labels there – Paulw11 Oct 29 '15 at 03:09
  • Okay now the static labels are gone and it's just blank – JoshyJay Oct 29 '15 at 03:15

1 Answers1

0

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

   }
jjjjjjjj
  • 4,203
  • 11
  • 53
  • 72