簡體   English   中英

幾秒后解雇UIAlertController?

[英]Dismiss UIAlertController after few seconds?

我需要顯示一個沒有提交按鈕的框,但讓它在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];   

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

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

});

使用alertController添加performSelector並以gobally創建UIAlertController對象

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

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

你可以這樣試試:

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

繼來自@ 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
            }];    
        }
    });
}]; 

在斯威夫特...

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

您可以使用此代碼來關閉您的UIAlertController 您需要全局聲明UIAlertController

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

你的選擇方法

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

您也可以嘗試這樣:對於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)
    }

最好的方法是將UIAlertController子類UIAlertController到您自己的類,並在viewDidAppear中添加NSTimer以自動關閉。

不要忘記invalidate計時器invalidate並在viewDidDisappear方法中釋放它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM