[英]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.