将值传递给另一个视图控制器(键值对forms的值)

在我的tableview ,我正在加载tableviewcells &值显示在那些单元格就像这样…

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: sellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "abc") as! sellTableViewCell let product = arrProduct?[indexPath.section] cell.rateTextField.text = product?.theRate //Showing images in imageView using SDWebImage cell.prdImgView.sd_setImage(with: arrProduct?[indexPath.section].images[indexPath.row].url, placeholderImage: UIImage(named: "appLogo.jpg"), options: []) { (image, error, imageCacheType, imageUrl) in self.appDelegate.commonArrayOfSelectedImages.append(image!)}} 

现在, self.appDelegate.commonArrayOfSelectedImages具有所有图像。 这些图片我可以在另一个视图控制器中显示。

但是随着图像,我也想要传递每个图像(在cell.rateTextField.text显示)的cell.rateTextField.text到另一个视图,以便我可以得到我的图像,也是与每个图像关联的速度。 这些费率如何与我无法弄清的图像一起传递。 希望有人能帮助…

创build一个你想传递你的数组值的数组。

绑定UITableViewDelegate

添加UITableViewDelegate的didselect方法

 optional public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ } 

假设你的数组是arrAnotherVC,viewcontroller将是AnotherVC。

 let anotherVC = AnotherVC! let product = arrProduct?[indexPath.section] anotherVC.arrAnotherVC = arrProduct?[indexPath.section] 

所以,你可以在AnotherVC中获得你的值。

谢谢!!!

我相信使用AppDelegate在ViewControllers之间传递数据不是一个好办法。

如果你想在两个ViewController之间传递date,我build议你应该使用准备for segue。 这是一个例子。

  1. 您应该在Storyboard中的Views中添加Segue。 不要在button和第二个视图之间创buildsegue,而是从第一个视图到第二个视图。

  2. 你应该为segue执行准备并执行segue方法:

首先:

  class SomeUIViewController: UIViewController { var someData: Int = 123 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath as IndexPath) tableView.deselectRow(at: indexPath as IndexPath, animated: true) performSegue(withIdentifier: "Show the second class", sender: cell) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "Show the second class" { if let secondVC = segue.destination as? AnotherUIViewController { secondVC.anotherData = self.someData } } } } 

第二:

 class AnotherUIViewController: UIViewController { var anotherData: Int? override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "Show the second class" { if let secondVC = segue.destination as? AnotherUIViewController { secondVC.anotherData = self.someData } } } } 

你应该修改commonArrayOfSelectedImages数组是封装图像和评级的对象数组,如:

 class CommonImage { let image: UIImage var rate: String? //create custom inits } 

每次访问commonArrayOfSelectedImages数组的元素时,您将获得CommonImage对象,其中每个图像都将具有关联的评级。

为了传递图像和文本,你可以创build一个Class对象:

 class imageData: NSObject { var image: UIImage? var data: String? } 

然后将值存储在已定义对象的数组中:

 var imageDataArray = [imageData]() 

为了添加新的数据:

 Let imgData = imageData() imgData.image = //your image imgData.data = //your text imageDataArray.append(imgData) //adding the data to array 

现在你可以通过segue方法将这个数组传递给你的viewcontroller。

尝试这个…

1.创build带有两个键的字典,并为ex赋值 图像和率两个关键。

2.然后在你的数组中添加这个字典并传递这个数组。

 let temDic = NSMutableDictionary() temDic.setValue(product?.theRate, forKey: "rate") temDic.setValue(image!, forKey: "image") self.appDelegate.commonArrayOfSelectedImages.add(temDic)