UIAlertController deviates after only 2 seconds

I want to make a dismissal immediately after the presentation, but I can only do it after 2 seconds. How to do it?

Here is my method that is called from UITapGestureRecognizer on 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:^{ // }]; } 
+4
source share
3 answers

Would something like that be easiest?

 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 want you to also not allow the user to press the cancel button until 2 seconds are triggered?

+8
source

That should work. It worked for me

 let alert = UIAlertController(title: "some title", message: "some message", preferredStyle: .Alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) dispatch_async(dispatch_get_main_queue()) { () -> Void in dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(10 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), { () -> Void in alert.dismissViewControllerAnimated(true, completion: nil) }) } 
+3
source

I am firing a UIAlertController with a timeout using the extension. Here is the code.

 extension UIViewController { func dismissViewController(timer: Timer) { let viewController = timer.userInfo as! UIViewController if (viewController.isViewLoaded && (viewController.view.window != nil)) { viewController.dismiss(animated: true, completion: nil) } } func presentWithTOT(_ viewControllerToPresent: UIViewController, timeout: Double) { if (timeout != 0.0) { Timer.scheduledTimer(timeInterval: timeout, target: self, selector: #selector(dismissViewController), userInfo: viewControllerToPresent, repeats: false) } present(viewControllerToPresent, animated: true, completion: nil) } } func myfunc() { let alertController = UIAlertController(...) presentWithTOT(alertController, timeout: 2.0) } 

One note for the above code is that the timer has a strong link to alertController, which means the object will not be released before the timeout expires.

0
source

Source: https://habr.com/ru/post/1208724/


All Articles