[英]How do I send data from a button click in a uicollectionview inside of a popover to a table cell in a main view controller?
[英]Send variable value to next view controller when click a table cell
我有两个表视图控制器
InvoiceList
视图控制器 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拖动(请看屏幕截图)
然后将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.