简体   繁体   中英

Including protocol function in tapGesture action selector - iOS Swift

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?

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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