繁体   English   中英

单击表格单元格时,将变量值发送到下一个视图控制器

[英]Send variable value to next view controller when click a table cell

我有两个表视图控制器

  1. InvoiceList视图控制器
  2. InvoiceShow视图控制器

我使用didSelectRowAtIndexPath方法作为波纹管来获取选定的表格单元didSelectRowAtIndexPath定值

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     let rowObject = objects[indexPath.row]
     let invoicehash = rowObject["hash_key"]!
}

单击InvoiceList的表格单元格时,我需要将invoicehash值发送到InvoiceShow控制器

我试图使用prepareForSegue函数。 但这并不适用,因为它将在didSelectRawAtIndexPath函数之前触发。 因此,当我实现它时,会给出先前的click事件变量值。 不正确的一个。

请帮助我从InvoiceShow控制器访问invoiceHash变量值

如果需要和/或已在情节提要中设置,则仍可以使用segue。 您只需要将Interface Builder中的两个视图控制器直接彼此连接即可。 因此,从控制器本身而不是从TableViewCell开始ctrl拖动(请看屏幕截图)

按住Ctrl键从ViewController拖动到ViewController

然后将performSegueMethod与新的segue标识符一起使用,如下所示:

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

最后,您的prepareForSegue方法:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "mySegueIdentifier" {
        let selectedIndex = self.invoiceTableView.indexPathForSelectedRow
        //if element exist
        if selectedIndex?.row < myDataSourceArray.count {
            let destination = segue.destinationViewController as! InvoiceShowViewController
            let invoice = myDataSourceArray[selectedIndex!.row]
            destination.invoice = invoice
        }
    }
}

而已!

您将在prepareForSegue方法本身中获得选定的单元格。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  let selectedIndexPath = self.tableView.indexPathForSelectedRow()!

  let rowObject = objects[selectedIndexPath.row]
  let invoiceHash = rowObject["hash_key"]!

  let invoiceShowViewController = segue.destinationViewController as! InvoiceShowViewController

  // Set invoiceHash to `InvoiceShowViewController ` here
  invoiceShowViewController.invoiceHash = invoiceHash
}

暂无
暂无

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

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