Reject the UIAlertController after a few seconds?

I need to show a window without a submit button, but let it disappear after 3 seconds. Can I set 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];   
+4
source share
7 answers

Maybe the code will work below:

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
        }];

});
+5
source

add performSelectorusing your alertController and create the 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];
}
+5
source

:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)),     dispatch_get_main_queue(), ^{
    [alert dismissViewControllerAnimated:YES completion:nil];
});
+3

, UIAlertController. UIAlertController .

[self performSelector:@selector(removeAlert) withObject:nil afterDelay:3];

-(void)removeAlert{
    [self dismissViewControllerAnimated:alert completion:nil];
}
+3

@RonakChaniyara, , ( , , ).

[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
            }];    
        }
    });
}]; 

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)
    }
}
+2

: 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];
    }

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)
    }
0

UIAlertController, , viewDidAppear a NSTimer .

invalidate viewDidDisappear

-1

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


All Articles