Reputation:
I need to show a box without submit button but let it disappear after 3 second. Is it possible to attach any timeout?
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Please wait, checking your PAID INVOICE, to allow the service."
message:"...waiting..."
preferredStyle:UIAlertControllerStyleAlert];
[self.window.rootViewController presentViewController:alert animated:YES
completion:nil];
Upvotes: 9
Views: 8840
Reputation: 8453
You can also try this: For Objective-C
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertController animated:YES completion:^{
[self performSelector:@selector(dismissAlertController:) withObject:alertController afterDelay:3.0];
}];
- (void)dismissAlertController:(UIAlertController *)alertController {
[alertController dismissViewControllerAnimated:YES completion:nil];
}
For Swift 3
let alertController = UIAlertController(title: "Title", message: "message", preferredStyle: .alert)
present(alertController, animated: true) {
self.perform(#selector(ViewController.dismissAlertController(alertController:)), with: alertController, afterDelay: 3.0)
}
internal func dismissAlertController(alertController: UIAlertController) {
alertController.dismiss(animated: true, completion: nil)
}
Upvotes: 1
Reputation: 53161
Following on from @RonakChaniyara's answer, this adds a test that the alert is still presented (if using with an alert that has a button to dismiss, for example).
[presentViewController:alert animated:YES completion: {
// Dispatch 3 seconds after alert presented
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// Check that alert is still presented
if self.presentedViewController == alert {
// Dismiss if it is
[self.dismissViewControllerAnimated:YES completion:^{
//Dismissed
}];
}
});
}];
and in Swift…
let alert = UIAlertController(title: "Please Wait", message: "…waiting…", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
present(alert, animated: true) {
DispatchQueue.main.asyncAfter(deadline: .now() + 3) { [weak self] in
guard self?.presentedViewController == alert else { return }
self?.dismiss(animated: true, completion: nil)
}
}
Upvotes: 5
Reputation: 82769
add performSelector
with your alertController and create your UIAlertController object as gobally
[self performSelector:@selector(hideAlertView) withObject:nil afterDelay:3.0];
-(void)hideAlertView{
[alert dismissViewControllerAnimated:YES completion:nil]; // or use [self dismissViewControllerAnimated:alert completion:nil];
}
Upvotes: 4
Reputation: 831
You can use this code to dismiss your UIAlertController
. You need to declare UIAlertController
globally.
[self performSelector:@selector(removeAlert) withObject:nil afterDelay:3];
Your selector method
-(void)removeAlert{
[self dismissViewControllerAnimated:alert completion:nil];
}
Upvotes: 2
Reputation: 5436
May below code will do the work:
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:@"Please wait, checking your PAID INVOICE, to allow the service."
message:"...waiting..."
preferredStyle:UIAlertControllerStyleAlert];
[self.window.rootViewController presentViewController:alert animated:YES
completion:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:^{
//Dismissed
}];
});
Upvotes: 9
Reputation: 5586
The best way would be to subclass UIAlertController
, to your own class, and in the viewDidAppear
, add a NSTimer
to dismiss automatically.
Don't forget to invalidate
the timer and to release it in the viewDidDisappear
method
Upvotes: 0
Reputation: 59
You may try it like this:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
Upvotes: 4