简体   繁体   English

Swift 变量在使用#selector时在其自己的初始值内使用

[英]Swift Variable used within its own initial value while using #selector

I'm trying to invoke my method from selector (action) but getting error Variable used within its own initial value Below is my code snippet我正在尝试从选择器(操作)调用我的方法,但在其自己的初始值中使用了错误变量下面是我的代码片段

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(sendRequest(tapGestureRecognizer, userID)))

Below is the method which I'm calling以下是我正在调用的方法

@objc func sendRequest(tapGestureRecognizer: UITapGestureRecognizer, identifer: String) {
   print("hello world")
}

My method accepts 2 paramters.我的方法接受 2 个参数。 I don't know how to call it.我不知道怎么称呼它。 The way I'm currently calling the sendRequestMethod is throwing error.我当前调用 sendRequestMethod 的方式是抛出错误。 Please help me to get it resolved.请帮我解决它。

when those functions are inside an UIView, as GestureRecognizers usually are, then you can make a hittest and find what UIView was under your touch.当这些功能在 UIView 中时,就像 GestureRecognizers 通常那样,那么您可以进行最热门的测试并找到 UIView 在您的触摸下。
Consider this is not the ideal way to catch touched views.考虑到这不是捕捉触摸视图的理想方式。 As every UIView also offers you a .tag you can set a numbering and use it to make assumtions to whom the hit UIView fits.由于每个 UIView 还为您提供了一个.tag ,您可以设置一个编号并使用它来假设 UIView 适合哪些人。 See Example查看示例

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)
        }
    }
}

Hint: Much easier is to subclass UIView and prepare a property that holds UserID given when allocation is done.提示:更容易的是继承 UIView 并准备一个属性来保存分配完成时给定的 UserID。 addtarget: action:#selector() to each of them. addtarget: action:#selector()到他们每个人。 The action/selector method gets invoked and the sender is passed in and because you know the type of the sender (YourUserUIView) you find the UserID.调用操作/选择器方法并传入发送者,因为您知道发送者的类型 (YourUserUIView),所以您可以找到用户 ID。

With GestureRecognizer's you tend to write recognition code that can become very complex the more complex your UI will be, instead of just passing objects to actions that tell you what you are looking for.使用 GestureRecognizer,您倾向于编写识别代码,如果您的 UI 越复杂,识别代码就会变得越复杂,而不仅仅是将对象传递给告诉您正在查找的内容的操作。

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

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