Reputation: 11651
I have a parent viewController
and a childviewController
named MyController
.
I would like to show the childview for some seconds and then dismiss it.
How do I dismiss the childs viewcontroller from the parents view controller ?
My code so far:
let mainStoryboard = UIStoryboard(name: "Storyboard", bundle: NSBundle.mainBundle())
let vc : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("MyController") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
<<wait for some seconds>>
-->> dismissview from parents view ???
Upvotes: 3
Views: 755
Reputation: 52093
You can use dispatch_after
with some time interval to delay the execution of the block:
let seconds: NSTimeInterval = 5.0
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC)))
dispatch_after(delay, dispatch_get_main_queue()) {
vc.dismissViewControllerAnimated(true, completion: nil)
}
You can define it as a function in case you might need it somewhere else in your program:
func dispatch_async_main_queue(delay: NSTimeInterval, _ block: dispatch_block_t) {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
dispatch_after(time, dispatch_get_main_queue(), block)
}
--
dispatch_async_main_queue(5.0) {
vc.dismissViewControllerAnimated(true, completion: nil)
}
Upvotes: 3