在警报控制器标题文本上设置倒数计时器
Set a Countdown timer on Alert Controller Title text
我有一个警报控制器,它需要几分钟的输入并显示控制器标题的倒计时,还会突出显示所选的操作。
如何更新标题上的计时器,并使选定的操作加粗。
我的警报控制器代码是:-
let actionSheet = UIAlertController(title: "Set Timer", message: "--:--", preferredStyle: .actionSheet)
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let time10 = UIAlertAction(title: "10 Minutes", style: .default) {
action in
self.totalTime = 600
self.startTimer()
}
let time20 = UIAlertAction(title: "20 Minutes", style: .default) {
action in
self.totalTime = 1200
self.startTimer()
}
let timeCustom = UIAlertAction(title: "Custom", style: .default) {
action in
self.performSegue(withIdentifier: "customTimer", sender: nil)
}
actionSheet.addAction(time10)
actionSheet.addAction(time20)
actionSheet.addAction(timeCustom)
actionSheet.addAction(cancel)
present(actionSheet, animated: true, completion: nil)
}
// startTimer()
func startTimer() {
countdownTimer?.invalidate()
countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}
@objc func updateTime() {
print("update:- \(totalTime)")
if totalTime != 0 {
totalTime -= 1
} else {
endTimer()
}
}
func endTimer() {
print("end:- \(totalTime)")
countdownTimer?.invalidate()
print("timer end")
}
将动作声明为实例 属性 并作为警报,用剩余时间更新消息,并在动作中定义首选动作:
let actionSheet = UIAlertController?
var totalTime = 600
var time10 : UIAlertAction?
var time20 : UIAlertAction?
var timeCustom : UIAlertAction?
并且在动作初始化期间:
actionSheet = UIAlertController(title: "Set Timer", message: "--:--", preferredStyle: .alert)
time10 = UIAlertAction(title: "10 Minutes", style: .default) {
action in
self.totalTime = 600
self.startTimer()
self.actionSheet?.preferredAction = self.time10
}
time20 = UIAlertAction(title: "20 Minutes", style: .default) {
action in
self.totalTime = 1200
self.startTimer()
self.actionSheet?.preferredAction = self.time20
}
let timeCustom = UIAlertAction(title: "Custom", style: .default) {
action in
self.performSegue(withIdentifier: "customTimer", sender: nil)
self.actionSheet?.preferredAction = self. and timertimeCustom
}
@objc func updateTime() {
print("update:- \(totalTime)")
if totalTime != 0 {
totalTime -= 1
} else {
endTimer()
}
let hour = totalTime / 3600
let minute = (totalTime - hour) / 60
let seconds = (totalTime - hour) % 60
actionSheet?.message = "\(hour):\(minute):\(seconds)"
}
第一次通话:
第二次通话:
Ptit Xav 的回答很好,但是有问题。当我第二次打开警报时应用程序崩溃。
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'UIAlertController can
only have one action with a style of UIAlertActionStyleCancel'
terminating with uncaught exception of type NSException
我以某种方式解决了这个问题,这是一个小问题。
定义属性:-
let actionSheet : UIAlertController?
var totalTime = 600
var time10 : UIAlertAction?
var time20 : UIAlertAction?
var timeCustom : UIAlertAction?
然后在动作函数里面:-
func showActionSheetForSelectTimer() {
actionSheet = UIAlertController(title: "Set Timer", message: "--:--", preferredStyle: .actionSheet)
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
time10 = UIAlertAction(title: "10 Minutes", style: .default) {
action in
self.totalTime = 600
self.startTimer()
self.actionSheet?.preferredAction = self.time10
}
time20 = UIAlertAction(title: "20 Minutes", style: .default) {
action in
self.totalTime = 1200
self.startTimer()
self.actionSheet?.preferredAction = self.time20
}
let timeCustom = UIAlertAction(title: "Custom", style: .default) {
action in
self.performSegue(withIdentifier: "customTimer", sender: nil)
self.actionSheet?.preferredAction = self. and timertimeCustom
}
actionSheet?.addAction(cancel)
actionSheet?.addAction(time10!)
actionSheet?.addAction(time20!)
actionSheet?.addAction(time30!)
actionSheet?.addAction(time1!)
actionSheet?.addAction(timeNever!)
actionSheet?.addAction(timeCustom!)
present(actionSheet!, animated: true, completion: nil)
}
@objc func updateTime() {
print("update:- \(totalTime)")
if totalTime != 0 {
totalTime -= 1
} else {
endTimer()
}
let hour = totalTime / 3600
let minute = (totalTime - hour) / 60
let seconds = (totalTime - hour) % 60
actionSheet.message = "\(hour):\(minute):\(seconds)"
}
我有一个警报控制器,它需要几分钟的输入并显示控制器标题的倒计时,还会突出显示所选的操作。
如何更新标题上的计时器,并使选定的操作加粗。 我的警报控制器代码是:-
let actionSheet = UIAlertController(title: "Set Timer", message: "--:--", preferredStyle: .actionSheet)
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let time10 = UIAlertAction(title: "10 Minutes", style: .default) {
action in
self.totalTime = 600
self.startTimer()
}
let time20 = UIAlertAction(title: "20 Minutes", style: .default) {
action in
self.totalTime = 1200
self.startTimer()
}
let timeCustom = UIAlertAction(title: "Custom", style: .default) {
action in
self.performSegue(withIdentifier: "customTimer", sender: nil)
}
actionSheet.addAction(time10)
actionSheet.addAction(time20)
actionSheet.addAction(timeCustom)
actionSheet.addAction(cancel)
present(actionSheet, animated: true, completion: nil)
}
// startTimer()
func startTimer() {
countdownTimer?.invalidate()
countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}
@objc func updateTime() {
print("update:- \(totalTime)")
if totalTime != 0 {
totalTime -= 1
} else {
endTimer()
}
}
func endTimer() {
print("end:- \(totalTime)")
countdownTimer?.invalidate()
print("timer end")
}
将动作声明为实例 属性 并作为警报,用剩余时间更新消息,并在动作中定义首选动作:
let actionSheet = UIAlertController?
var totalTime = 600
var time10 : UIAlertAction?
var time20 : UIAlertAction?
var timeCustom : UIAlertAction?
并且在动作初始化期间:
actionSheet = UIAlertController(title: "Set Timer", message: "--:--", preferredStyle: .alert)
time10 = UIAlertAction(title: "10 Minutes", style: .default) {
action in
self.totalTime = 600
self.startTimer()
self.actionSheet?.preferredAction = self.time10
}
time20 = UIAlertAction(title: "20 Minutes", style: .default) {
action in
self.totalTime = 1200
self.startTimer()
self.actionSheet?.preferredAction = self.time20
}
let timeCustom = UIAlertAction(title: "Custom", style: .default) {
action in
self.performSegue(withIdentifier: "customTimer", sender: nil)
self.actionSheet?.preferredAction = self. and timertimeCustom
}
@objc func updateTime() {
print("update:- \(totalTime)")
if totalTime != 0 {
totalTime -= 1
} else {
endTimer()
}
let hour = totalTime / 3600
let minute = (totalTime - hour) / 60
let seconds = (totalTime - hour) % 60
actionSheet?.message = "\(hour):\(minute):\(seconds)"
}
第一次通话:
第二次通话:
Ptit Xav 的回答很好,但是有问题。当我第二次打开警报时应用程序崩溃。
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIAlertController can only have one action with a style of UIAlertActionStyleCancel' terminating with uncaught exception of type NSException
我以某种方式解决了这个问题,这是一个小问题。
定义属性:-
let actionSheet : UIAlertController?
var totalTime = 600
var time10 : UIAlertAction?
var time20 : UIAlertAction?
var timeCustom : UIAlertAction?
然后在动作函数里面:-
func showActionSheetForSelectTimer() {
actionSheet = UIAlertController(title: "Set Timer", message: "--:--", preferredStyle: .actionSheet)
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
time10 = UIAlertAction(title: "10 Minutes", style: .default) {
action in
self.totalTime = 600
self.startTimer()
self.actionSheet?.preferredAction = self.time10
}
time20 = UIAlertAction(title: "20 Minutes", style: .default) {
action in
self.totalTime = 1200
self.startTimer()
self.actionSheet?.preferredAction = self.time20
}
let timeCustom = UIAlertAction(title: "Custom", style: .default) {
action in
self.performSegue(withIdentifier: "customTimer", sender: nil)
self.actionSheet?.preferredAction = self. and timertimeCustom
}
actionSheet?.addAction(cancel)
actionSheet?.addAction(time10!)
actionSheet?.addAction(time20!)
actionSheet?.addAction(time30!)
actionSheet?.addAction(time1!)
actionSheet?.addAction(timeNever!)
actionSheet?.addAction(timeCustom!)
present(actionSheet!, animated: true, completion: nil)
}
@objc func updateTime() {
print("update:- \(totalTime)")
if totalTime != 0 {
totalTime -= 1
} else {
endTimer()
}
let hour = totalTime / 3600
let minute = (totalTime - hour) / 60
let seconds = (totalTime - hour) % 60
actionSheet.message = "\(hour):\(minute):\(seconds)"
}