【发布时间】:2018-05-02 01:11:20
【问题描述】:
我使用 Swift 4 编写并尝试编写一个将 categoryId 作为操作发送的函数,但我无法编写。我认为我的语法是错误的。如果我编写不带参数的函数,这不是问题,但参数函数会出错.你能告诉我如何使用选择器吗?
@objc func sendCategoryIdToPackageSelectionVC(categoryId : Int){
MarketVC.categoryId = categoryId
self.performSegue(withIdentifier: "sequeGoToPackageSelection", sender: nil)
}
func addTapFeatures(){
taplabel1 = UITapGestureRecognizer(target: self, action: #selector(self.sendCategoryIdToPackageSelectionVC(categoryId:2)))
taplabel1?.cancelsTouchesInView = false
self.labelFirst.addGestureRecognizer(taplabel1!)
}
我收到一条错误消息,提示操作选择器未引用任何 objc 方法。
【问题讨论】:
-
检查this thread 或this。 Swift 4 中关于
#selector的使用没有什么新鲜事。 -
func sendCategoryIdToPackageSelectionVC(categoryId : Int){ self.performSegue(withIdentifier: "sequeGoToPackageSelection", sender: nil) } @objc func callMethod(){ sendCategoryIdToPackageSelectionVC(categoryId: 2) } func addTapFeatures(){ taplabel1 = UITapGestureRecognizer(target: self, action: #selector(self.callMethod)) taplabel1?.cancelsTouchesInView = false }
-
谢谢大家,我发现我的观点是错误的。我将再次使用相同的功能,但是这次我在 objc 功能中使用 sender : UILabel,然后我控制 sender.tag 哪个标签属于该标签。我根据该标签发送 categoryId 谢谢你的帮助。