繁体   English   中英

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

[英]How to pass data from one UIViewController to another one through UITapGestureRecognizer

我有一个自定义的UIView卡形式。 现在,我在其上放置了一个轻击手势识别器,以便当我的用户单击它时,它将移动到选定的视图控制器。 我已经能够实现这一目标,并且运行良好。 现在我的问题是我想通过此操作传递数据,以便新的视图控制器可以访问数据。 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM