I want to dismiss an UIAlertView after a couple of seconds (without need of an 'ok' button).
After some studying I found that I could use dispatch_after as the delay to eventually dismiss the alert:
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC)) // ...Int64.init() struct in Swift.
),
dispatch_get_main_queue(), closure)
}
Source: Using dispatch_after vs NSTimer
The above code works fine, but I would like to edit the closure to allow passage of a sender:UIViewController parameter. This is where I'm getting confused.
The dispatch_after() format is:
func dispatch_after(_ when: dispatch_time_t, _ queue: dispatch_queue_t, _ block: dispatch_block_t)
Here is what I tried (which is WRONG):
func delay(delay:Double, closure:(sender:UIViewController)->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC)) // ...Int64.init() struct in Swift.
),
dispatch_get_main_queue(), closure:(sender:UIViewController) in {
})
}
Question: how to I fix this to have a block/closure parameter containing a callback reference ('sender:UIViewController')?