I\'ve got an app where I push a UIAlertController
with several custom UIAlertAction
s. Each UIAlertAction
performs unique tasks in the
With a bit of clever casting, I've found a way to do this in Swift (2.2):
extension UIAlertController {
typealias AlertHandler = @convention(block) (UIAlertAction) -> Void
func tapButtonAtIndex(index: Int) {
let block = actions[index].valueForKey("handler")
let handler = unsafeBitCast(block, AlertHandler.self)
handler(actions[index])
}
}
This allows you to call alert.tapButtonAtIndex(1)
in your test and have the correct handler executed.
(I would only use this in my test target, btw)