简体   繁体   English

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

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

I have two table view controllers 我有两个表视图控制器

  1. InvoiceList view controller InvoiceList视图控制器
  2. 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拖动(请看屏幕截图)

按住Ctrl键从ViewController拖动到ViewController

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.

相关问题 如何将数据从弹出窗口内 uicollectionview 中的按钮单击发送到主视图控制器中的表格单元格? - 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? 当我在集合视图中单击按钮时,我无法将值传递给下一个视图控制器 - when I click button in collection view, i I can't pass value to next view controller 选择表视图控制器单元格时不会出现视图控制器 - View Controller will not appear when Table View Controller Cell is Selected 将多个选定表单元格中的数组传递到下一个视图控制器 - Pass the array in multiple selected table cell into next view controller 当我选择单元格时如何向另一个视图控制器发送一些值? - How to send some value to another view controller when i select cell? 表单元进入视图控制器 - Table cell into view controller 如何在表格视图单元格中单击按钮时获取标签值 - How to get label value in table view cell on button click in cell 如何将值从表视图单元格传递到视图控制器 - how to pass the value from table view cell to view controller 将数组发送到下一个视图控制器 - send an array to next view controller 游戏结束时,我需要将持有分数的变量“发送”到表视图控制器 - When my game ends I need to 'send' the variable holding the score to a table view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM