简体   繁体   English

如何在Swift 2.0中使用prepareForSegue将数据从PFTableViewCell传递到下一个ViewController?

[英]How do I pass the data from PFTableViewCell to the next ViewController using prepareForSegue in swift 2.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. 我试图将数据从PFTableViewCell传递到我的下一个视图控制器(详细信息),但是我仍然看不到数据。 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. 我认为我已经为Swift 2.0 / Xcode 7正确编写了我的代码。我已经坚持了大约2周,而且我一点也不擅长编码。 Is there any other code that could pass the data to other viewcontroller? 是否还有其他代码可以将数据传递给其他ViewController?

In my FirstViewController I have this code written: 在我的FirstViewController中,我编写了以下代码:

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: 在我的DetailsViewController中,我这样写:

    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"
}

ok if you are using a PFQueryTableViewController, try this: 好的,如果您使用的是PFQueryTableViewController,请尝试以下操作:

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. 1)从PFQueryTableViewController(不是从单元格,从视图控制器)到要将数据传输到的视图控制器创建手动选择。 In the inspector panel in the interface builder, name it "yourManualSegue" 在界面构建器的检查器面板中,将其命名为“ yourManualSegue”

2) at the top of your class (pf the PFQueryTableViewController), create 2 variables: 2)在类的顶部(PFQueryTableViewController),创建2个变量:

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

3) call the didSelectRowAtIndexPath method, like so: 3)调用didSelectRowAtIndexPath方法,如下所示:

  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: 4)现在,在您的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

   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 斯威夫特:如何传递子视图控制器prepareForSegue的数据以更改父视图控制器表视图单元格的TextLabel? - Swift: How can I pass data from child viewcontroller prepareForSegue to change the TextLabel of a cell of parent viewcontroller tableview? 稍后在应用中使用prepareForSegue将数据传递到ViewController - Using prepareForSegue to pass data to ViewController later in app 如何使用Swift 3将数据从ViewController A传递到另一个ViewController B - How to pass data from viewcontroller A to another viewcontroller B using swift 3 iOS swift prepareForSegue无法在两个ViewController之间传递数据 - iOS swift prepareForSegue cannot pass data between two viewcontroller 如何将计时器中的数据传递给新的ViewController(Swift 3) - How do I pass data in timer to new ViewController (Swift 3) 如何使用prepareForSegue(Swift 4,Xcode 9)将UIButton的标签(一个Int)传递给其他ViewController - How to pass UIButton's tag (an Int) to other ViewController using prepareForSegue (Swift 4, Xcode 9) 使用prepareForSegue将图像文件名从一个ViewController发送到下一个ViewController - Using prepareForSegue to send an image file name from one ViewController to the next 如何以不同的方式在 prepareForSegue function 上传递数据? swift 5 - how pass data on prepareForSegue function differently? swift 5 如何将数据从selectedrow发送到下一个viewcontroller? - How do I send data from selectedrow to next viewcontroller? 如何将数据从模态呈现的 ViewController 传递到 swift 5 中的父 ViewController? - How to pass the data from modally presented ViewController to parent ViewController in swift 5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM