I want do dismiss immediately, after presented, but can do it only after 2 seconds. How to do this?
Here is my method which is called from UITapGestureRecognizer on a UILabel.
- (IBAction)labelTaped:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
CGRect frame = CGRectNull;
NSString *message = nil;
// ...
// some code
/// ...
if (message) {
// show info alert
__weak __typeof(self)weakSelf = self;
UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
message:message
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"alert_ok", @" - ")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
if ([weakSelf.dismissAlertTimer isValid]) {
[weakSelf.dismissAlertTimer invalidate];
}
}];
[alert addAction:cancelAction];
UIPopoverPresentationController *popoverController = alert.popoverPresentationController;
popoverController.sourceRect = frame;
popoverController.sourceView = sender.view;
[self.mainController presentViewController:alert animated:YES completion:^{
if ([weakSelf.dismissAlertTimer isValid]) {
[weakSelf.dismissAlertTimer invalidate];
}
weakSelf.dismissAlertTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:weakSelf selector:@selector(dismissAlertController) userInfo:nil repeats:NO];
}];
}
}
}
- (void)dismissAlertController {
[self.mainController dismissViewControllerAnimated:YES completion:^{
//
}];
}
Would the easiest be something like this?
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"message dismiss in 2 seconds" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertController animated:YES completion:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[alertController dismissViewControllerAnimated:YES completion:^{
// do something ?
}];
});
Or do you mean you also want to prevent the user from tapping on the cancel button before the 2 second is triggered ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With