[英]Swift Variable used within its own initial value while using #selector
我正在尝试从选择器(操作)调用我的方法,但在其自己的初始值中使用了错误变量下面是我的代码片段
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(sendRequest(tapGestureRecognizer, userID)))
以下是我正在调用的方法
@objc func sendRequest(tapGestureRecognizer: UITapGestureRecognizer, identifer: String) {
print("hello world")
}
我的方法接受 2 个参数。 我不知道怎么称呼它。 我当前调用 sendRequestMethod 的方式是抛出错误。 请帮我解决它。
当这些功能在 UIView 中时,就像 GestureRecognizers 通常那样,那么您可以进行最热门的测试并找到 UIView 在您的触摸下。
考虑到这不是捕捉触摸视图的理想方式。 由于每个 UIView 还为您提供了一个.tag ,您可以设置一个编号并使用它来假设 UIView 适合哪些人。 查看示例
class checkCheckView : UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(sendRequest(recognizer:identifer:)) )
@objc func sendRequest(recognizer:UITapGestureRecognizer, identifer:String) {
print("hello world")
// your problem will be, how is identifier passed in here?
}
let PanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panning(gesture:)))
@objc func panning(gesture:UIPanGestureRecognizer) {
//print("pan-translation %@", gesture.translation(in: self).debugDescription)
let location = gesture.location(ofTouch: 0, in: self)
//print("pan-location %@",location.debugDescription);
let hitview = self.hitTest(location, with: nil)
if hitview != nil {
let UserIDTag = hitview?.tag ?? 0
print("userId by UIView tag",UserIDTag)
}
}
}
提示:更容易的是继承 UIView 并准备一个属性来保存分配完成时给定的 UserID。 addtarget: action:#selector()
到他们每个人。 调用操作/选择器方法并传入发送者,因为您知道发送者的类型 (YourUserUIView),所以您可以找到用户 ID。
使用 GestureRecognizer,您倾向于编写识别代码,如果您的 UI 越复杂,识别代码就会变得越复杂,而不仅仅是将对象传递给告诉您正在查找的内容的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.