【发布时间】:2015-08-06 13:29:52
【问题描述】:
我正在展示一个没有任何按钮的 UIAlertViewController,因为它应该只是通知用户上传正在进行中。该应用程序应该将一些文件上传到 Amazon S3(有些事情发生在并行线程上),我担心当我想关闭它时,对警报视图控制器的引用会丢失。
知道什么地方出了问题吗? 我什至不知道如何调试,因为调试区域没有错误?
我有一个类级别的属性:var uploadInProgressAlert = UIAlertController()
我使用此代码在没有按钮的情况下显示我的警报(它有效):
self.uploadInProgressAlert = UIAlertController(title: "Uploading", message: "Please wait.", preferredStyle: .Alert)
self.presentViewController(self.uploadInProgressAlert, animated: true, completion: nil)
此代码用于解除警报(警报不会被解除):
self.uploadInProgressAlert.dismissViewControllerAnimated(false, completion: nil)
在此线程中:iOS dismiss UIAlertController in response to event 有人谈到“持有参考”。我不知道“保留参考”是什么意思,我认为这可能是问题的根源。
编辑:我把上面的代码放在一个简单的测试应用程序中,它就可以工作了。但是当一些并行线程变得复杂时,我找不到解除警报的方法。
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)
}
【问题讨论】:
-
持有引用就是你正在做的——让 alert 成为一个实例变量
-
持有一个引用正是你通过声明一个类属性
uploadInProgressAlert所做的。你确定解雇这条线曾经被叫过吗? -
是的,我确定该行被调用了,因为我在该行之前和之后有一些 println()。
标签: ios swift cocoa-touch