ios 如何在没有任何按钮的情况下以编程方式关闭 UIAlertController?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31857137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programmatically dismiss UIAlertController without any buttons?
提问by Andrej
I'm presenting an UIAlertViewController without any buttons, as it is supposed to just inform users that uploading is in progress. The app is supposed to upload some files to Amazon S3 (some things happen on parallel threads) and I'm afraid that the reference to the alert view controller gets lost when I want to dismiss it.
我正在展示一个没有任何按钮的 UIAlertViewController,因为它应该只是通知用户上传正在进行中。该应用程序应该将一些文件上传到 Amazon S3(有些事情发生在并行线程上),我担心当我想关闭它时,对警报视图控制器的引用会丢失。
Any idea on what could be wrong? I even don't know how to debug this since there's no error in the Debug area?
关于可能出什么问题的任何想法?我什至不知道如何调试它,因为调试区域没有错误?
I have a class level property: var uploadInProgressAlert = UIAlertController()
我有一个类级属性: var uploadInProgressAlert = UIAlertController()
I use this code to present my alert without buttons (it works):
我使用此代码在没有按钮的情况下显示我的警报(它有效):
self.uploadInProgressAlert = UIAlertController(title: "Uploading", message: "Please wait.", preferredStyle: .Alert)
self.presentViewController(self.uploadInProgressAlert, animated: true, completion: nil)
This code is to dismiss the alert (the alert doesn't get dismissed):
self.uploadInProgressAlert.dismissViewControllerAnimated(false, completion: nil)
此代码用于解除警报(警报不会被解除):
self.uploadInProgressAlert.dismissViewControllerAnimated(false, completion: nil)
In this thread: iOS dismiss UIAlertController in response to eventsomeone talked about "holding the reference". I don't know what it means "hold the reference" and I think this could be the root of the problem.
在此线程中:iOS 关闭 UIAlertController 以响应有人谈到“持有引用”的事件。我不知道“持有参考”是什么意思,我认为这可能是问题的根源。
EDIT:I've put the above code in a simple test app and there it works. But when things get complicated with some parallel threads I can't find a way to dismiss the alert.
编辑:我已经把上面的代码放在一个简单的测试应用程序中,它可以工作。但是当某些并行线程变得复杂时,我找不到解除警报的方法。
var delay4s = NSTimer()
var delay8s = NSTimer()
var alert = UIAlertController()
func showAlert() {
if NSClassFromString("UIAlertController") != nil {
alert = UIAlertController(title: "Uploading", message: "Please wait! \n\n", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
}
}
func dismissAlert(){
self.alert.dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
delay4s = NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: "showAlert", userInfo: nil, repeats: false)
delay8s = NSTimer.scheduledTimerWithTimeInterval(8.0, target: self, selector: "dismissAlert", userInfo: nil, repeats: false)
}
回答by JiuJitsuCoder
Generally the parent view controller is responsible for dismissing the modally presented view controller (your popup). In Objective-C you would do something like this in the parent view controller:
通常,父视图控制器负责关闭模态呈现的视图控制器(您的弹出窗口)。在 Objective-C 中,你会在父视图控制器中做这样的事情:
[self dismissViewControllerAnimated:YES completion:nil];
The same code in Swift versions < 3 would be:
Swift 版本 < 3 中的相同代码将是:
self.dismissViewControllerAnimated(true, completion: nil)
Swift 3.0:
斯威夫特 3.0:
self.dismiss(animated: true, completion: nil)
回答by sandeep jaglan
for swift you can do:
对于 swift 你可以这样做:
nameOfYourAlertController.dismiss(animated: true, completion: nil)
nameOfYourAlertController.dismiss(animated: true, completion: nil)
true will animate the disappearance, and false will abruptly remove the alert
true 将动画消失,false 将突然删除警报
回答by Adrian
If you want to post an alert that displays briefly, then dismisses itself, you could use the following method:
如果您想发布一个短暂显示的警报,然后自行解除,您可以使用以下方法:
func postAlert(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
self.present(alert, animated: true, completion: nil)
// delays execution of code to dismiss
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0, execute: {
alert.dismiss(animated: true, completion: nil)
})
}
回答by ylgwhyh
Use the alertController's own method to destroy itself.
使用 alertController 自己的方法来销毁自己。
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:...];
[alertController dismissViewControllerAnimated:YES completion:nil];
回答by Stotch
Nothing above seemed to work, but here is what works for me perfectly (xcode 10, swift 5). Enjoy!
上面似乎没有任何效果,但这里对我来说完美无缺(xcode 10,swift 5)。享受!
Step 1: Place this is your viewController Class
第 1 步:放置这是您的 viewController 类
var newQuestionAlert:UIAlertController?
Step 2: Create function to show alert
第 2 步:创建函数以显示警报
func ShowNewQuestionPopup() {
if newQuestionAlert == nil {
newQuestionAlert = UIAlertController(title: "Notice", message: "Next Question Starting", preferredStyle: .alert)
if let newQuestionAlert = newQuestionAlert {
newQuestionAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
self.newQuestionAlert = nil
return
}))
self.present(newQuestionAlert, animated: true, completion: nil)
}
}
}
Step 3: Create function to dismiss alert
第 3 步:创建函数以解除警报
func autoDismiss() {
newQuestionAlert?.dismiss(animated: false, completion: nil)
newQuestionAlert = nil
}
Step 4: Call functions as needed
第 4 步:根据需要调用函数