Including protocol function in tapGesture action selector - iOS Swift

故事扮演 提交于 2019-12-12 02:53:51

问题


I have a class (FilterBar.swift) and a viewcontroller. the viewcontroller calls the class to populate a scrollView object with list of images and each image has a tapGestureRecognizer added to it as follows:

        let tapGesture = UITapGestureRecognizer(
                        target: imgView, action: Selector("filterClicked:"));
        tapGesture.numberOfTapsRequired = 1;
        tapGesture.numberOfTouchesRequired = 1;
        imgView.addGestureRecognizer(tapGesture);

and i have a function also in FilterBar as follows:

    @objc func filterClicked(sender: UITapGestureRecognizer) {
    print(sender.view?.superview?.description)
    print("sent from view: \(sender.view!.tag)");
}

when i attempt to click on the image i get the following error:

2016-03-16 02:06:45.800 ImageFilter[71811:6885004] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView delegate.filterClicked:]: unrecognized selector sent to instance 0x7fc07951f010'

So i thought maybe i need a protocol, so i created a protocol in FilterBar as follows:

protocol FilterClickedDelegate {
    func filterClicked(sender: UITapGestureRecognizer);
}

let delegate:FilterClickedDelegate?

(adjusted this line) let tapGesture = UITapGestureRecognizer(
             target: imgView, action: Selector("delegate.filterClicked:"));

i added that protocol to the viewcontroller class, and i also added the function the protocol needs but i still get the same error. So first question: Am i right to use a protocol in this senario since this is running on viewcontroller?

second question: what am i doing wrong in the selector? is there a specific way to mention a protocol function in the selector?


回答1:


The last line of your post code:

(adjusted this line) let tapGesture = UITapGestureRecognizer(
             target: imgView, action: Selector("delegate.filterClicked:"));  

I think should be:

(adjusted this line) let tapGesture = UITapGestureRecognizer(
         target: delegate, action: Selector("filterClicked:"));  

I think it will work.
But I don't suggest doing in this way, it's some kind strange. What I usually do is:

(adjusted this line) let tapGesture = UITapGestureRecognizer(
         target: self, action: Selector("filterClicked:"));   

And If I really need to inform delegate to do something, I will call delegate to do it in the filterClicked:. Just like:

In the ViewController:

fun filterClicked() {
// delegate to do something
delegate.xxx()
}



回答2:


I figured it out, in the target section when initializing the UITapGestureRecognizer you need to use the viewcontroller which is actually the delegate of the delegator. so:

let tapGesture = UITapGestureController(target: vc, action:"filterClicked:");

Thanks all, and hope this will help others stuck on the same issue!

cheers!



来源:https://stackoverflow.com/questions/36025243/including-protocol-function-in-tapgesture-action-selector-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!