是否可以创建一个UIAlertController,它具有一个最初被禁用的按钮,然后启用5然后是3然后3..2..1。
我希望用户实际读取控制器内部的消息,我觉得这样做会非常烦人,实际上阻止他们盲目地点击确定
如果可能,我将如何开始这样做?
谢谢
答案 0 :(得分:2)
因为我们试图修改UIAlertAction
的只读属性,所以它很小。但它对我来说很好。或者,您可以创建看起来像UIAlertController的自定义视图控制器:
var timer: NSTimer?
var timerCount: Int = 5
func showAlertView() {
let alertController = UIAlertController(title: "Title", message: "Message of alert", preferredStyle: .Alert)
let okAction = UIAlertAction(title: "Ok", style: .Default, handler: nil)
okAction.enabled = false
alertController.addAction(okAction)
alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
presentViewController(alertController, animated: true) {
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(self.countDownTimer), userInfo: nil, repeats: true)
}
}
func countDownTimer() {
timerCount -= 1
let alertController = presentedViewController as! UIAlertController
let okAction = alertController.actions.first
if timerCount == 0 {
timer?.invalidate()
timer = nil
okAction?.setValue("Ok", forKey: "title")
okAction?.enabled = true
} else {
okAction?.setValue("Ok \(timerCount)", forKey: "title")
}
}
这就是它的样子:
答案 1 :(得分:0)
是,您可以通过以下方式在特定时间间隔后启用警报操作:
func showAlert() {
let alertview = UIAlertController(title: "title", message: "msg", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertview.addAction(action)
self.presentViewController(alertview, animated: true) { () -> Void in
action.enabled = false
NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "enableBtn:", userInfo: action, repeats: false)
}
}
func enableBtn(t: NSTimer){
let info = t.userInfo as! UIAlertAction
info.enabled = true
}
希望它会对你有所帮助:)。