如何使用 UIAlertController 在 Swift 暂停代码并等待 UIAlert 的回答
How to pause the code and wait for an answer from UIAlert on Swift using UIAlertController
我不得不说我已经在堆栈溢出中搜索了答案,但找不到,所以这就是我问它的原因...
**
- 我的问题:
我的问题是 - 我正在制作一个番茄钟应用程序,我让
用户设置 TimeLabel(时间),现在如果用户在会话中,
他想改变他能够做到的时间,尽管如果他
尝试这样做,一旦他改变了时间并想取消
它,用户当然能够做到,现在的问题是我
使用 UiAlertController 等待用户的回答,但其中
我还使用更新设置的功能
Labels/Parameters.
**
我的代码:
我的更新设置函数:
//Updating the UserSettings:
func updateSettingsLabels(cell: PickerTableViewCell, rowIndex: Int) {
let identifier = cell.reuseIdentifier
switch identifier {
case T.Identifiers.workIntervalID:
workInterval.text = timesDictionary[rowIndex]
self.workIntervalString = timesDictionary[rowIndex] // set string
let myString = workInterval.text!
oldTime = workInterval.text! // setting up the old time
saveTime(labelText: myString)
case T.Identifiers.shortBreakID:
shortBreak.text = timesDictionary[rowIndex]
self.shortBreakString = timesDictionary[rowIndex] // set string
saveShortBreak(breakText: shortBreak.text!)
case T.Identifiers.longBreakTimeID:
longBreakTime.text = timesDictionary[rowIndex]
self.longBreakString = timesDictionary[rowIndex] // set string
saveLongBreak(longBreakText: longBreakString!)
case T.Identifiers.breakAfterIntervalsID: // after how much intervals we will take a long break
longBreakAfter.text = intervalsDictionary[rowIndex]
self.longBreakAfterIntervals = intervalsDictionary[rowIndex] // set string
saveBreakAfterIntervals(breakAfterIntervals: longBreakAfterIntervals!)
case T.Identifiers.dailyIntervalsID: // daily intervals
dailyIntervals.text = intervalsDictionary[rowIndex]
self.dailyIntervalsString = intervalsDictionary[rowIndex] // set string
saveDailyIntervals(dailyIntervals: dailyIntervalsString!)
default:
print("There was a problem with Switch")
}
我的 UiAlert 函数:
func alertTheUser() {
//If we want to print something on the screen, we use UIAlertController:
let alert = UIAlertController(title: "Do you want to aboard this task?", message: "Once you change the time in a middle of a session, you will reset all tasks.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (alertToCancel) in
self.userWantToChangeSettings = false
print("Im being changed!")
}))
alert.addAction(UIAlertAction(title: "Ok", style: .destructive, handler: { (action) in
self.userWantToChangeSettings = true
}))
self.present(alert, animated: true)
}
我必须告诉你们,我也使用 UIPicker 让用户从可选时间中选择时间,这是我的代码:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int, forCell cell: PickerTableViewCell) {
if self.timerIsOnFire == true {
self.alertTheUser()
}
updateSettingsLabels(cell: cell, rowIndex: row)
self.delegate?.userSettings(interval: self.workIntervalString, shortBreak:self.shortBreakString, longBreak:self.longBreakString, breakAfterIntervals:self.longBreakAfterIntervals,dailyIntervals:self.dailyIntervalsString )
self.view.endEditing(true)
}
像这样将 Alert 函数与完成处理程序一起使用
func alertTheUser(completion:@escaping ()->Void) {
//If we want to print something on the screen, we use UIAlertController:
let alert = UIAlertController(title: "Do you want to aboard this task?", message: "Once you change the time in a middle of a session, you will reset all tasks.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (alertToCancel) in
self.userWantToChangeSettings = false
print("Im being changed!")
completion()
}))
alert.addAction(UIAlertAction(title: "Ok", style: .destructive, handler: { (action) in
self.userWantToChangeSettings = true
completion()
}))
self.present(alert, animated: true)
}
我不得不说我已经在堆栈溢出中搜索了答案,但找不到,所以这就是我问它的原因...
**
- 我的问题: 我的问题是 - 我正在制作一个番茄钟应用程序,我让 用户设置 TimeLabel(时间),现在如果用户在会话中, 他想改变他能够做到的时间,尽管如果他 尝试这样做,一旦他改变了时间并想取消 它,用户当然能够做到,现在的问题是我 使用 UiAlertController 等待用户的回答,但其中 我还使用更新设置的功能 Labels/Parameters.
**
我的代码: 我的更新设置函数:
//Updating the UserSettings:
func updateSettingsLabels(cell: PickerTableViewCell, rowIndex: Int) {
let identifier = cell.reuseIdentifier
switch identifier {
case T.Identifiers.workIntervalID:
workInterval.text = timesDictionary[rowIndex]
self.workIntervalString = timesDictionary[rowIndex] // set string
let myString = workInterval.text!
oldTime = workInterval.text! // setting up the old time
saveTime(labelText: myString)
case T.Identifiers.shortBreakID:
shortBreak.text = timesDictionary[rowIndex]
self.shortBreakString = timesDictionary[rowIndex] // set string
saveShortBreak(breakText: shortBreak.text!)
case T.Identifiers.longBreakTimeID:
longBreakTime.text = timesDictionary[rowIndex]
self.longBreakString = timesDictionary[rowIndex] // set string
saveLongBreak(longBreakText: longBreakString!)
case T.Identifiers.breakAfterIntervalsID: // after how much intervals we will take a long break
longBreakAfter.text = intervalsDictionary[rowIndex]
self.longBreakAfterIntervals = intervalsDictionary[rowIndex] // set string
saveBreakAfterIntervals(breakAfterIntervals: longBreakAfterIntervals!)
case T.Identifiers.dailyIntervalsID: // daily intervals
dailyIntervals.text = intervalsDictionary[rowIndex]
self.dailyIntervalsString = intervalsDictionary[rowIndex] // set string
saveDailyIntervals(dailyIntervals: dailyIntervalsString!)
default:
print("There was a problem with Switch")
}
我的 UiAlert 函数:
func alertTheUser() {
//If we want to print something on the screen, we use UIAlertController:
let alert = UIAlertController(title: "Do you want to aboard this task?", message: "Once you change the time in a middle of a session, you will reset all tasks.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (alertToCancel) in
self.userWantToChangeSettings = false
print("Im being changed!")
}))
alert.addAction(UIAlertAction(title: "Ok", style: .destructive, handler: { (action) in
self.userWantToChangeSettings = true
}))
self.present(alert, animated: true)
}
我必须告诉你们,我也使用 UIPicker 让用户从可选时间中选择时间,这是我的代码:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int, forCell cell: PickerTableViewCell) {
if self.timerIsOnFire == true {
self.alertTheUser()
}
updateSettingsLabels(cell: cell, rowIndex: row)
self.delegate?.userSettings(interval: self.workIntervalString, shortBreak:self.shortBreakString, longBreak:self.longBreakString, breakAfterIntervals:self.longBreakAfterIntervals,dailyIntervals:self.dailyIntervalsString )
self.view.endEditing(true)
}
像这样将 Alert 函数与完成处理程序一起使用
func alertTheUser(completion:@escaping ()->Void) {
//If we want to print something on the screen, we use UIAlertController:
let alert = UIAlertController(title: "Do you want to aboard this task?", message: "Once you change the time in a middle of a session, you will reset all tasks.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (alertToCancel) in
self.userWantToChangeSettings = false
print("Im being changed!")
completion()
}))
alert.addAction(UIAlertAction(title: "Ok", style: .destructive, handler: { (action) in
self.userWantToChangeSettings = true
completion()
}))
self.present(alert, animated: true)
}