如何将数据从表视图控制器中以编程方式传递到另一个控制器?[重复]

问题描述 投票:0回答:1

我的目标是使它成为一个用户从表视图中选择一个选项时,它将转到下一个控制器,并显示特定的图像和他们选择的选项的信息。是否有一个简单的修复方法,我只是错过了?

ios swift uitableview uikit
1个回答
0
投票

在你的目标控制器创建一个变量的相同类型的变量,你要传递,并使用没有选择的方法导航到目标控制器和传递的数据,像下面。

func tableView(_ tableView: UITableView, didSelectRowA indexPath: IndexPath) {
    let nextVC = DestinationVC()
    nextVC.info = yourArray[indexPath.row].info
    nextVC.modallyPresentingStyle = .fullscreen
    self.present( nextVC,  animated: true,  completion: nil)
}

传递所有你想要的数据,比如信息数据,不要忘了在目标控制器上创建具有相同声明类型的变量,例如,如果你想传递一个图像,那么在你的目标VC上创建一个图像变量,如下图所示。

 var selectedImage = UIImage()

然后将其添加到之前的方法中

nextVC.selectedImage = yourImage // if not part of a cell at index path
nextVC.selectedImage = yourArray[indexpath.row].yourImage // if it is related to specific cell at index path
© www.soinside.com 2019 - 2024. All rights reserved.