I want to define action for my KCFloatingActionButton. UITapGestureRecognizer is defined for the fab button in ViewController, but I want to do this action in manager class or on another page. I got the KCFloatingActionButton view, but I can't give it a click action.When I click on KCFloatingActionButton I don't want to define a new UITapGestureRecognizer with addGestureRecognizer ,I want it to take the action it is defined.
I can do this for UIButton or UIBarButtonItem as follows
button.sendActions(for: .touchUpInside)
barButtonItem.target?.perform(barButtonItem.action, with: nil)
How can I do this action for KCFloatingActionButton ?
You can try something like this:
button.addTarget(self, action: #selector(handleButtonAction), for: .touchUpInside)
@objc func handleButtonAction(){
//Put your logic here
}
The self
in addTarget
refers where will be the action to be executed, in this case will be in the current ViewController.
The #selector(handleButtonAction)
is the function that will be executed when the user touch the button, in this case will be an Objective-C function (that's why is use the "#selector").
The .touchUpInside
is the event that has to be triggered to execute the function.
And finally the function itself @objc func handleButtonAction() { }
will execute all the actions that you define for the touch up event.
the @objc
attribute comes in: when you apply it to a class or method it instructs Swift to make those things available to Objective-C as well as Swift code.