如何以编程方式解除UIAlertController没有任何按钮? [英] How to programmatically dismiss UIAlertController without any buttons?

查看:102
本文介绍了如何以编程方式解除UIAlertController没有任何按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在呈现一个没有任何按钮的UIAlertViewController,因为它只是告知用户上传正在进行中。该应用程序应该将一些文件上传到Amazon S3(某些事情发生在并行线程上),我担心当我想解雇时,对警报视图控制器的引用会丢失。

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.

有什么可能出错的想法吗?
我甚至不知道如何调试这个,因为调试区域没有错误?

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?

我有一个类级属性: 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)

此代码用于解除警报(警报不会被解雇):
self.uploadInProgressAlert.dismissViewControllerAnimated(false,completion:nil)

This code is to dismiss the alert (the alert doesn't get dismissed): self.uploadInProgressAlert.dismissViewControllerAnimated(false, completion: nil)

在这个帖子中: iOS解雇UIAlertController以响应活动有人谈到持有参考。我不知道持有参考是什么意思,我认为这可能是问题的根源。

In this thread: iOS dismiss UIAlertController in response to event someone 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.

编辑:我已经将上面的代码放在一个简单的测试应用程序中,并且它有效。但是当一些并行线程的事情变得复杂时,我找不到解除警报的方法。

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


推荐答案

通常父视图控制器负责解除模态显示的视图控制器(弹出窗口)。在Objective-C中,您可以在父视图控制器中执行以下操作:

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:

[self dismissViewControllerAnimated:YES completion:nil];

Swift版本中的相同代码< 3将是:

The same code in Swift versions < 3 would be:

self.dismissViewControllerAnimated(true, completion: nil)

Swift 3.0:

Swift 3.0:

self.dismiss(animated: true, completion: nil)

这篇关于如何以编程方式解除UIAlertController没有任何按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆