我认为我想使用户可以交互,当用户触摸他时,他会执行对话。为什么代码不起作用。
@IBOutlet weak var HistoryCategory: UIView!
var historyTapGesture: UIGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
self.historyTapGesture = UIGestureRecognizer(target: nil, action: #selector(chooseCategoryViewController.openHistoryQuest(tap:)))
HistoryCategory.isUserInteractionEnabled = true
HistoryCategory.addGestureRecognizer(historyTapGesture)
// Do any additional setup after loading the view.
}
@objc func openHistoryQuest(tap: UIGestureRecognizer) {
performSegue(withIdentifier: "history", sender: nil)
}
答案 0 :(得分:1)
请不要忘记您的视图控制器类是执行Segue的类,因此您需要将“ self”设置为目标和发送者,并如@rmaddy所述将其设置为轻击手势识别器。
只需替换这两行:
self.historyTapGesture = UIGestureRecognizer(target: nil, action: #selector(chooseCategoryViewController.openHistoryQuest(tap:)))
performSegue(withIdentifier: "history", sender: nil)
使用:
self.historyTapGesture = UITapGestureRecognizer(target: self, action: #selector(chooseCategoryViewController.openHistoryQuest(tap:)))
performSegue(withIdentifier: "history", sender: self)