首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将数据从单元格传递到另一个ViewController

如何将数据从单元格传递到另一个ViewController
EN

Stack Overflow用户
提问于 2021-09-27 09:05:50
回答 2查看 103关注 0票数 1

@TusharMordiya请检查这张图片我在视图的CollectionView.The内容中创建了一个UIImage和一个UILabel.I,我想设计一个单元格,当我单击一个单元格时,图像和标签必须转到另一个ViewController。

代码语言:javascript
复制
import UIKit

class ExploreTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{

   @IBOutlet var collectionView: UICollectionView!

   var namearr = ["images-1", "images-2", "images-3", "images-4", "images-5"]

   override func awakeFromNib() {
      super.awakeFromNib()
    
      collectionView.delegate = self
      collectionView.dataSource = self

   }
   override func setSelected(_ selected: Bool, animated: Bool) {
      super.setSelected(selected, animated: animated)

   }

   func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      return 10
   }



   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
      return cell
   }

   func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
      return CGSize(width: 132, height: 134)
   }


   func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
      let vc = UIStoryboard(name: "DetailViewController", bundle: nil).instantiateViewController(withIdentifier:"DetailViewController") as? DetailViewController
    
      vc?.name = namearr[indexPath.row]
      vc?.img.image = UIImage(named: namearr[indexPath.row])
      //self.navigationController?.pushViewController(vc, animated: true)
    
      // showing error in here and while declaring object of viewcontroller
   }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-09-30 05:03:30

请尝尝这个。

  1. 添加函数以获取最顶层视图控制器 扩展UIApplication { class func topViewController(_ viewController: UIViewController?= UIApplication.shared.connectedScenes .filter({$0.0.activationState == .foregroundActive}) .compactMap({$0 as?.filter({$0.isKeyWindow}).first?.rootViewController) -> UIViewController?{如果让nav = viewController?UINavigationController {返回topViewController(nav.visibleViewController) }如果让选项卡= viewController as?UITabBarController {如果让选择= tab.selectedViewController {返回topViewController(选定)}}如果让呈现= viewController?.presentedViewController {返回topViewController(呈现)}返回viewController }
  2. didSelectItemAt方法中使用此代码 func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) { let storyBoard =UIStoryboard(名称:"Main",bundle: nil)如果让vc =collectionView as?UIApplication.topViewController()?.navigationController?.pushViewController(vc,{ vc.name = namearrindexPath.row print(“您用汽车名(namearrindexPath.row)点击单元格(IndexPath) ") vc.name:true}}
  3. 在你的详细屏幕上 类DetailViewController: UIViewController { @IBOutlet lbl: UILabel!@IBOutlet img: UIImageView!变量名称= "“覆盖func viewDidLoad() { super.viewDidLoad() lbl.text = name img.image =UIImage(命名: name) }}
票数 0
EN

Stack Overflow用户

发布于 2021-09-30 04:47:57

代码语言:javascript
复制
    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier:"DetailViewController") as? DetailViewController

将storyBoard名称更改为Main

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69344007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档