I want to hook up a UIButton to a piece of code – from what I have found, the preferred method to do this in Swift is still to use the addTarget(target: AnyObject?, ac
This is easily solved using RxSwift
import RxSwift
import RxCocoa
...
@IBOutlet weak var button:UIButton!
...
let taps = button.rx.tap.asDriver()
taps.drive(onNext: {
// handle tap
})
Edit:
I wanted to acknowledge that RxSwift/RxCocoa is a pretty heavy-weight dependency to add to a project just to solve this one requirement. There may be lighter-weight solutions available or just stick with target/action pattern.
In any case, if the idea of a general purpose declarative approach to handling application and user events appeals to you, definitely give RxSwift a look. It's the bomb.