关于 ios:使 UIAlertViewController 动作不可点击?

Make UIAlertViewController action unclickable?

我想让 UIAlertAction 无法点击。基本上,当我尝试下载某些东西时,我会弹出一个 UIAlertView 。下载完成后,我希望 UIAlertView's action 从不可点击变为可点击。当它变为可点击时,表示下载已完成。这是我到目前为止所拥有的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@IBOutlet var activityView: UIActivityIndicatorView!

var alert = UIAlertController(title: nil, message:"Please wait...", preferredStyle: UIAlertControllerStyle.Alert)

override func viewDidLoad(){
    self.activityView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    self.activityView.center = CGPointMake(130.5, 65.5)
    self.activityView.color = UIColor.blackColor()
}

@IBAction func importFiles(sender: AnyObject){
    self.alert.view.addSubview(activityView)
    self.presentViewController(alert, animated: true, completion: nil)
    alert.addAction(UIAlertAction(title:"Ok", style: UIAlertActionStyle.Default, handler: nil))
    //Somehow here I want to make it so that the UIAlertAction is unclickable.
    self.activityView.startAnimating()
}

一切正常,但我似乎无法找到一种方法让动作只有在动画完成后才能点击。我尝试将操作添加到 UIAlertViewController 并在下载完成后再次呈现 UIAlertViewController 但我收到一个错误消息,指出当 UIAlertViewController 已经处于活动状态时我无法呈现某些内容。任何帮助,将不胜感激。谢谢!


一般来说,我建议不要使用警报视图作为此类任务的占位符。但为了给你一个想法,看看这个:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
lazy var alert: UIAlertController = {
    var alert = UIAlertController(title:"Please wait...", message:"Please wait for some seconds...", preferredStyle: .Alert)
    alert.addAction(self.action)
    return alert
}()
lazy var action: UIAlertAction = {
    var action = UIAlertAction(title:"Ok", style: .Default, handler: nil)
    action.enabled = false
    return action
}()

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.presentViewController(alert, animated: true, completion: nil)

    let delayInSeconds = 3.0
    let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC)))
    dispatch_after(popTime, dispatch_get_main_queue()) { () -> Void in
        self.action.enabled = true
    }
}

延迟部分在哪里

1
2
3
4
5
let delayInSeconds = 3.0
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC)))
dispatch_after(popTime, dispatch_get_main_queue()) { () -> Void in
    self.action.enabled = true
}

应该由你的获取逻辑替换。

祝你好运!