如何以编程方式解除UIAlertController没有任何button?

我提出一个UIAlertViewController没有任何button,因为它应该只是通知用户正在进行上传。 该应用程序应该上传一些文件到Amazon S3(有些事情发生在并行线程上),恐怕当我想解雇它的时候,对alert视图控制器的引用会丢失。

任何想法可能是错的? 我甚至不知道如何debugging,因为在debugging区没有错误?

我有一个类级别的属性: var uploadInProgressAlert = UIAlertController()

我使用此代码来显示我的警报没有button(它的工作原理):

 self.uploadInProgressAlert = UIAlertController(title: "Uploading", message: "Please wait.", preferredStyle: .Alert) self.presentViewController(self.uploadInProgressAlert, animated: true, completion: nil) 

此代码是closures警报(警报不会被驳回): self.uploadInProgressAlert.dismissViewControllerAnimated(false, completion: nil)

在这个线程: iOS解雇UIAlertController响应某人谈论“持有参考”的事件。 我不知道这意味着什么“保持参考”,我认为这可能是问题的根源。

编辑:我已经把上面的代码在一个简单的testing应用程序,它在那里工作。 但是,当一些并行线程变得复杂时,我无法find解决警报的方法。

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

通常,父视图控制器负责解除模态显示的视图控制器(您的popup窗口)。 在Objective-C中,你可以在父视图控制器中这样做:

 [self dismissViewControllerAnimated:YES completion:nil]; 

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

 self.dismissViewControllerAnimated(true, completion: nil) 

Swift 3.0:

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

为了迅速,你可以这样做:

nameOfYourAlertController.dismissViewControllerAnimated(true, completion: nil)

真实会消失,虚假会突然消除警报

使用alertController自己的方法来销毁自己。

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:...]; [alertController dismissViewControllerAnimated:YES completion:nil];