sending a parameter argument to function through UITapGestureRecognizer selector

前端 未结 1 1990
深忆病人
深忆病人 2021-01-03 00:13

I am making an app with a variable amount of views all with a TapGestureRecognizer. When the view is pressed, i currently am doing this

func addView(headline         


        
相关标签:
1条回答
  • 2021-01-03 00:19

    Instead of creating a generic UITapGestureRecognizer, subclass it and add a property for the headline:

    class MyTapGestureRecognizer: UITapGestureRecognizer {
        var headline: String?
    }
    

    Then use that instead:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let gestureRecognizer = MyTapGestureRecognizer(target: self, action: "tapped:")
        gestureRecognizer.headline = "Kilroy was here."
        view1.addGestureRecognizer(gestureRecognizer)
    }
    
    func tapped(gestureRecognizer: MyTapGestureRecognizer) {
        if let headline = gestureRecognizer.headline {
            // Do fun stuff.
        }
    }
    

    I tried this. It worked great.

    0 讨论(0)
提交回复
热议问题