首页 > 解决方案 > Swift 将数据发送到不同故事板中的视图控制器

问题描述

我正在尝试将对象发送到不同情节提要中的 Viewcontroller。

Todo 所以我尝试了这个,但它总是为零:

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let link = list[indexPath.row].link
    self.object.cat = link

  let storyboard = UIStoryboard(name: "Main", bundle: nil)
  let controller = storyboard.instantiateViewController(withIdentifier: "SelectContacts")
  self.present(controller, animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destVC = segue.destination as! SelectContactsViewController
    destVC.object = object
}

标签: swiftuicollectionview

解决方案


你需要

let controller = self.storyboard!.instantiateViewController(withIdentifier: "SelectContacts") as! SelectContactsViewController
controller.object = link
self.present(controller, animated: true, completion: nil)

prepareForSegue使用时触发performSegue


每个 vc 都包含引用主故事板的故事板属性,因此不需要

let storyboard = UIStoryboard(name: "Main", bundle: nil)

推荐阅读