[英]Send variable value to next view controller when click a table cell
I have two table view controllers 我有两个表视图控制器
InvoiceList
view controller InvoiceList
视图控制器 InvoiceShow
view controller InvoiceShow
视图控制器 I use didSelectRowAtIndexPath
method as bellow to get selected table cell specific value 我使用
didSelectRowAtIndexPath
方法作为波纹管来获取选定的表格单元didSelectRowAtIndexPath
定值
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let rowObject = objects[indexPath.row]
let invoicehash = rowObject["hash_key"]!
}
i need to send invoicehash
value to InvoiceShow
controller when click the table cell of InvoiceList
单击
InvoiceList
的表格单元格时,我需要将invoicehash
值发送到InvoiceShow
控制器
i tried to use prepareForSegue
function. 我试图使用
prepareForSegue
函数。 but it is not applicable because it will trigger before the didSelectRawAtIndexPath
function. 但这并不适用,因为它将在
didSelectRawAtIndexPath
函数之前触发。 so when i implemented it, gives the previous click event variable value. 因此,当我实现它时,会给出先前的click事件变量值。 not correct one.
不正确的一个。
Please help me to access invoiceHash
variable value from InvoiceShow
controller 请帮助我从
InvoiceShow
控制器访问invoiceHash
变量值
You can still use a segue if you want and/or already setup on your storyboard. 如果需要和/或已在情节提要中设置,则仍可以使用segue。 You just need to connect the two view controllers in Interface Builder directly from one to another.
您只需要将Interface Builder中的两个视图控制器直接彼此连接即可。 So, start ctrl-dragging from the controller itself and not from the TableViewCell (take a look at the screenshot)
因此,从控制器本身而不是从TableViewCell开始ctrl拖动(请看屏幕截图)
then use the performSegueMethod with the new segue identifier like this: 然后将performSegueMethod与新的segue标识符一起使用,如下所示:
self.performSegueWithIdentifier("mySegueIdentifier", sender: self)
and finally, your prepareForSegue method: 最后,您的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
}
}
}
That's it! 而已!
You will get the selected cell in prepareForSegue
method itself. 您将在
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.