如何编程性地关闭UIAlertController没有任何按钮? [英] How to programatically dismiss UIAlertController without any buttons?

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

问题描述

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



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



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



我使用这个代码来表示我的提醒没有按钮(它的工作原理):



self.uploadInProgressAlert = UIAlertController(标题:上传,消息:请等待。,preferredStyle:.Alert)
self.presentViewController (self.uploadInProgressAlert,animated:true,completion:nil)

此代码是关闭警报该警报不会被解除):
self.uploadInProgressAlert.dismissViewControllerAnimated(false,completion:nil)



在此主题中: iOS关闭UIAlertController以回应事件有人谈到持有参考。我不知道这是什么意思持有参考,我认为这可能是问题的根源。



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

  var delay4s = NSTimer )
var delay8s = NSTimer()
var alert = UIAlertController()

func showAlert(){
if NSClassFromString(UIAlertController)!= nil {
alert = UIAlertController(title:Uploading,message:Please wait!\\\
\\\
,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中,你将在父视图控制器中执行这样的操作:

  [self dismissViewControllerAnimated:YES completion:nil]; 

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

  self.dismissViewControllerAnimated(true,completion:nil)
/ pre>

Swift 3.0:

  self.dismiss true,completion:nil)


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?

I have a class level property: 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)

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.

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

解决方案

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

The same code in Swift versions < 3 would be:

self.dismissViewControllerAnimated(true, completion: nil)

Swift 3.0:

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

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

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