簡體   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