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

时间:2018-07-28 13:42:37

标签: ios swift uiviewcontroller

我有一个自定义的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)
}

1 个答案:

答案 0 :(得分:0)

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

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