几秒后如何关闭 UIAlertController?

9
我需要展示一个没有提交按钮的框,但是让它在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];   
7个回答

8

下面的代码可能会起到作用:

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

});

1
这会导致重复的代码,如果需要多次使用该函数。 - CZ54
1
不错的想法,但是你应该将调度/解散代码添加到 presentViewControllercompletion: 块中。 - Ashley Mills

5

继@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)
    }
}

4
您可以尝试以下方法:

您可以像这样尝试:

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

1
这会导致重复的代码,如果需要多次使用该函数。 - CZ54

4

在你的alertController中添加performSelector,并将UIAlertController对象创建为全局对象。

 [self performSelector:@selector(hideAlertView) withObject:nil afterDelay:3.0];

-(void)hideAlertView{
 [alert dismissViewControllerAnimated:YES completion:nil];  // or use [self dismissViewControllerAnimated:alert completion:nil];
}

2
您可以使用此代码来关闭您的UIAlertController。 您需要全局声明UIAlertController
[self performSelector:@selector(removeAlert) withObject:nil afterDelay:3];

您的选择器方法
-(void)removeAlert{
    [self dismissViewControllerAnimated:alert completion:nil];
}

1
你也可以尝试这个: 对于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中添加一个NSTimer以自动解除。不要忘记在viewDidDisappear方法中使计时器无效并释放它。

糟糕的想法...你过于复杂化了事情! - Ashley Mills
1
为什么这样说?子类化过于复杂?重用代码也是如此吗?还有可测试性? - CZ54
很好的回答,但是提供一段代码示例会更好。 - arao6

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接