几秒后解雇UIAlertController?

8 objective-c ios uialertcontroller

我需要显示一个没有提交按钮的框,但让它在3秒后消失.是否可以附加任何超时?

  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];   
Run Code Online (Sandbox Code Playgroud)

Ron*_*ara 7

可以在代码下面完成工作:

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

});
Run Code Online (Sandbox Code Playgroud)


Ash*_*lls 5

根据@RonakChaniyara\'s 的回答,这添加了一个测试,以确保警报仍然存在(例如,如果与具有关闭按钮的警报一起使用)。

\n\n
[presentViewController:alert animated:YES completion: {\n\n    // Dispatch 3 seconds after alert presented\n    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{\n\n        // Check that alert is still presented\n        if self.presentedViewController == alert {\n            // Dismiss if it is\n            [self.dismissViewControllerAnimated:YES completion:^{    \n                //Dismissed\n            }];    \n        }\n    });\n}]; \n
Run Code Online (Sandbox Code Playgroud)\n\n

并在 Swift\xe2\x80\xa6 中

\n\n
let alert = UIAlertController(title: "Please Wait", message: "\xe2\x80\xa6waiting\xe2\x80\xa6", preferredStyle: .alert)\nalert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))\n\npresent(alert, animated: true) {\n    DispatchQueue.main.asyncAfter(deadline: .now() + 3) { [weak self] in\n        guard self?.presentedViewController == alert else { return }\n\n        self?.dismiss(animated: true, completion: nil)\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n