我需要显示一个没有提交按钮的框,但让它在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];
答案 0 :(得分: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
}];
});
答案 1 :(得分:5)
使用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 :(得分:4)
你可以这样试试:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
答案 3 :(得分:4)
继续来自@ 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 :(得分:3)
您可以使用此代码关闭UIAlertController
。您需要全局声明UIAlertController
。
[self performSelector:@selector(removeAlert) withObject:nil afterDelay:3];
您的选择方法
-(void)removeAlert{
[self dismissViewControllerAnimated:alert completion:nil];
}
答案 5 :(得分:0)
最好的方法是将UIAlertController
子类化到您自己的班级,并在viewDidAppear
中添加NSTimer
以自动关闭。
不要忘记invalidate
计时器并使用viewDidDisappear
方法将其释放
答案 6 :(得分:0)
你也可以试试这个: 对于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)
}