单击表格单元格时,将变量值发送到下一个视图控制器 [英] Send variable value to next view controller when click a table cell

查看:107
本文介绍了单击表格单元格时,将变量值发送到下一个视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表视图控制器


  1. InvoiceList 视图控制器

  2. InvoiceShow 视图控制器

code> didSelectRowAtIndexPath 方法获得选定的表格单元特定值

  override func tableView (tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath){
let rowObject = objects [indexPath.row]
let invoicehash = rowObject [hash_key]!

我需要发送 invoicehash InvoiceList

的表格单元格时, InvoiceShow 控制器

我试着用 prepareForSegue 函数。但它不适用,因为它会在 didSelectRawAtIndexPath 函数之前触发。所以当我实现它时,给出了以前的点击事件变量值。

请帮我从 InvoiceShow 变量值存取 invoiceHash code> controller

解决方案

您将在 prepareForSegue
$ b

 override func prepareForSegue(segue:UIStoryboardSegue,sender:AnyObject?){
let selectedIndexPath = self.tableView.indexPathForSelectedRow()!

let rowObject = objects [selectedIndexPath.row]
let invoiceHash = rowObject [hash_key]!

让invoiceShowViewController = segue.destinationViewController as! InvoiceShowViewController

//将invoiceHash设置为`InvoiceShowViewController`here
invoiceShowViewController.invoiceHash = invoiceHash
}


I have two table view controllers

  1. InvoiceList view controller
  2. InvoiceShow view controller

I use didSelectRowAtIndexPath method as bellow to get selected table cell specific value

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

i tried to use prepareForSegue function. but it is not applicable because it will trigger before the didSelectRawAtIndexPath function. so when i implemented it, gives the previous click event variable value. not correct one.

Please help me to access invoiceHash variable value from InvoiceShow controller

解决方案

You will get the selected cell in prepareForSegue method itself.

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
}

这篇关于单击表格单元格时,将变量值发送到下一个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆