ios - 如何通过 UITapGestureRecognizer 将数据从一个 UIViewController 传递到另一个 UIViewController

标签 ios swift uiviewcontroller

我有一个自定义的 UIView,它是卡片的形式。现在我在其上放置了一个点击手势识别器,以便当我的用户单击它时,它将移动到选定的 View Controller 。我已经实现了这一目标,而且效果很好。现在我的问题是我想通过此操作传递数据,以便新的 View Controller 可以访问数据。 prepareSegue 不起作用。下面是我的代码:

//Function to go to plans view controller
@objc func goToPlans(_sender: UITapGestureRecognizer) {
    let moveTo = storyboard?.instantiateViewController(withIdentifier: "ChosenOfferViewController")
    present(moveTo!, animated: true, completion: nil)
}

//Function to add gestures to the card
func addGesturesToCards() {
    let planRec = UITapGestureRecognizer(target: self, action: #selector(self.goToPlans))
    planCard.addGestureRecognizer(planRec)
}

最佳答案

试试这个。只需将 View Controller 转换为自定义类,然后以这种方式传递数据

guard let moveTo = storyboard?.instantiateViewController(withIdentifier: "ChosenOfferViewController") as? ChosenOfferViewController else { return }
moveTo.someProperty = yourData
present(moveTo, animated: true, completion: nil)

关于ios - 如何通过 UITapGestureRecognizer 将数据从一个 UIViewController 传递到另一个 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51571945/

相关文章:

ios - 将 URL 转换为 UIImage。能够转换但不能附加到 UIImage 数组

ios - 最适合互动书籍的 Controller 是什么?

iphone - 设置动画总是出现

objective-c - 在 Objective-C 中将八进制字符串转换为十进制?

ios - 从 PDFDocument iOS 获取页面大小

ios - 切换 UIViewControllers 后停止使用 NSTimer 更新 UILabel

iphone - 调整 uinavigationbar 推送/弹出 View Controller 大小问题

ios - 多个 SKPhysicsBody 来自带有 alpha 的纹理

ios - 无法构建 watchOS 扩展

ios - 自定义 UITableViewCell 中的 UIScrollView 混合数据