Question: I have built a Cordova alarm clock app that has a timer. Everything works good except that I now want to inform the user when the clock is up with a visual and audio alarm.

I have used the following plugin for local notifications: <https://github.com/katzer/cordova-plugin-local-notifications>

This seems to work good except that it doesn't seem to work when the phone screen is off.

Any ideas of a better way of doing this. I probably want something like AlarmManager in Android but so far I haven't found a Cordova plugin for it.

Answer: 
This is not necessarily a "hooking," but you can effectively achieve this behavior by subclassing UIButton:

```
class ActionButton: UIButton {
    var touchDown: ((button: UIButton) -> ())?
    var touchExit: ((button: UIButton) -> ())?
    var touchUp: ((button: UIButton) -> ())?

    required init?(coder aDecoder: NSCoder) { fatalError("init(coder:)") }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupButton()
    }

    func setupButton() {
        //this is my most common setup, but you can customize to your liking
        addTarget(self, action: #selector(touchDown(_:)), forControlEvents: [.TouchDown, .TouchDragEnter])
        addTarget(self, action: #selector(touchExit(_:)), forControlEvents: [.TouchCancel, .TouchDragExit])
        addTarget(self, action: #selector(touchUp(_:)), forControlEvents: [.TouchUpInside])
    }

    //actions
    func touchDown(sender: UIButton) {
        touchDown?(button: sender)
    }

    func touchExit(sender: UIButton) {
        touchExit?(button: sender)
    }

    func touchUp(sender: UIButton) {
        touchUp?(button: sender)
    }
}

```

Use:

```
let button = ActionButton(frame: buttonRect)
button.touchDown = { button in
    print("Touch Down")
}
button.touchExit = { button in
    print("Touch Exit")
}
button.touchUp = { button in
    print("Touch Up")
}

```
You can approach this with a proxy class that routes events through the target / action (selector) mechanism to a closure of your making. I have done this for gesture recognizers, but the same pattern should hold for controls.

You could do something like this:

```
import UIKit

@objc class ClosureDispatch {
    init(f:()->()) { self.action = f }
    func execute() -> () { action() }
    let action: () -> ()
}

var redBlueGreen:[String] = ["Red", "Blue", "Green"]
let buttons:[UIButton] = map(0..<redBlueGreen.count) { i in
    let text = redBlueGreen[i]
    var btn = UIButton(frame: CGRect(x: i * 50, y: 0, width: 100, height: 44))
    btn.setTitle(text, forState: .Normal)
    btn.setTitleColor(UIColor.redColor(), forState: .Normal)
    btn.backgroundColor = UIColor.lightGrayColor()
    return btn
}

let functors:[ClosureDispatch] = map(buttons) { btn in
    let functor = ClosureDispatch(f:{ [unowned btn] in
        println("Hello from \(btn.titleLabel!.text!)") })
    btn.addTarget(functor, action: "execute", forControlEvents: .TouchUpInside)
    return functor
}

```

The one caveat of this, is that since addTarget:... does not retain the target, you need to hold on to the dispatch objects (as done with the functors array). You don't strictly need to hold on to the buttons of course, as you could do that via a captured reference in the closure, but likely you will want explicit references.

PS. I tried to test this in playground, but could not get sendActionsForControlEvents to work. I have used this approach for gesture recognizers though.