I have a generic control class which needs to set the completion of the button depending on the view controller.Due to that setLeftButtonActionWithClosure function needs to take as parameter a closure which should be set as action to an unbutton.How would it be possible in Swift since we need to pass the function name as String to action: parameter.
func setLeftButtonActionWithClosure(completion: () -> Void)
{
self.leftButton.addTarget(<#target: AnyObject?#>, action: <#Selector#>, forControlEvents: <#UIControlEvents#>)
}
Similar solution to those already listed, but perhaps lighter weight:
Usage:
Or if avoiding retain loops:
Swift
After trying all the solutions, this one worked for me for all cases, even when the button in reusable table view cell
And then you call it like this:
Resource: https://medium.com/@jackywangdeveloper/swift-the-right-way-to-add-target-in-uibutton-in-using-closures-877557ed9455
Similar solution to those already listed, but perhaps lighter weight and doesn't rely on randomness to generate unique ids:
Usage:
Swift 4.2 for UIControl and UIGestureRecognizer, and and remove targets through swift extension stored property paradigm.
Wrapper class for the selector
Protocols with
associatedtype
s so we can hide hide theobjc_
codeExtension to make the property default and available
Let us apply the magic
And to the gestures
Example usage:
My solution.
Then at some point in your app's lifecycle, you'll mutate the instances' closure. Here's an example