Normal and long click on UIBarButtonItem

0

I try to implement a double functionality on a button of a navigation bar, for this I have implemented this code

class ViewController: UIViewController {

@IBOutlet weak var txtBuscar: UITextField!
@IBOutlet weak var btnNuevoItem: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBar.barStyle = UIBarStyle.blackTranslucent
    navigationController?.navigationBar.barTintColor = UIColor(red:0.02, green:0.63, blue:0.67, alpha:0.20)
    tabBarController?.tabBar.barTintColor = UIColor(red:0.02, green:0.63, blue:0.67, alpha:0.20)
    tabBarController?.tabBar.tintColor = UIColor.white
}     

@IBAction func btnNuevoItem_onClick(sender: Any, forEvent event: UIEvent) 
{
    guard let touch = event.allTouches?.first else { return }
    if touch.tapCount == 1 {
        Mensaje().mensajeSinFuncion(controller: self, texto: "1 click")
    } else if touch.tapCount == 0 {
        Mensaje().mensajeSinFuncion(controller: self, texto: "long click")
    }

}

}

The problem is the argument forEvent of method btnNuevoItem_onClick because removing it does not give me any error, the error mentioned is this

App.ViewController btnNuevoItem_onClick:]: unrecognized selector sent to instance 0x7fe36990be00
2017-10-19 11:17:05.675036-0500 App[4474:644996] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-       [App.ViewController btnNuevoItem_onClick:]: unrecognized selector sent to instance 0x7fe36990be00'

Any idea of controlling this error or any other way to achieve it?

    
asked by Kevtho 19.10.2017 в 19:01
source

1 answer

0

The problem seems to be that from the user interface the method is called with a single parameter, not with two as it is defined in your case.

At some point (in Objective-C at least) it worked with a @IBAction with two parameters, but I think this was never documented officially by Apple.

One option I can think of is to create the UIBarButtonItem with a custom view and handle the events in that view , perhaps using a gesture recognizer .

Anyway, it does not seem to be the best to have a button with tap and long tap at the same time in navigation bar .. The user expects that by doing tap some action, discovering that long tap has another effect can be difficult as well as counterproductive.

According to the Human Interface Guidelines :

  

As a general rule, use standard gestures . People are familiar with the   standard gestures and do not appreciate being forced to learn different   ways to do the same thing.

    
answered by 19.10.2017 в 20:37