UIAlertController中按钮内部显示的倒计时计时器,然后启用按钮swift
是否可以创建一个UIAlertController,该控制器有一个按钮,该按钮最初被禁用,然后有一个5,然后是4,然后是3..2..1,然后被启用 我希望用户能够真正阅读控制器内的消息,我认为这会让人非常恼火,从而阻止他们无意识地点击OK 如果可能的话,我将如何开始这样做UIAlertController中按钮内部显示的倒计时计时器,然后启用按钮swift,swift,uialertcontroller,Swift,Uialertcontroller,是否可以创建一个UIAlertController,该控制器有一个按钮,该按钮最初被禁用,然后有一个5,然后是4,然后是3..2..1,然后被启用 我希望用户能够真正阅读控制器内的消息,我认为这会让人非常恼火,从而阻止他们无意识地点击OK 如果可能的话,我将如何开始这样做 谢谢我们试图修改UIAlertAction的只读属性,这有点麻烦。然而,它对我来说很好。或者,您可以创建类似UIAlertController的自定义视图控制器: var timer: NSTimer? var timerCo
谢谢我们试图修改
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")
}
}
这就是它的样子:
我们试图修改
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")
}
}
这就是它的样子:
是的,您可以通过以下方式在特定间隔后启用警报操作:
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
}
希望它能帮助您:)是的,您可以通过以下方式在特定间隔后启用警报操作:
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
}
希望对您有所帮助:)非常感谢。以后会不会有麻烦?非常感谢。将来会有麻烦吗?