当我在 UIPickerView 上 select "done" 按钮显式 selecting 一行之前应用程序崩溃
App crashes when I select "done" button on UIPickerView before explicitly selecting a row
除非我明确地 select 一行,否则 UIPickerView 崩溃并且我得到错误
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'time interval must be greater than 0'
我知道它崩溃是因为它没有 select 任何一行,并且时间间隔的默认值为 0。
那么我怎样才能让 PickerView
到 select 第一行,而不必自己明确 select 呢?
相关代码如下:
var timerDisplayed = 0
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
timerDisplayed = Int(timeSelect[row])!
}
@objc func timeClock(){
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (didAllow, error) in }
let content = UNMutableNotificationContent()
content.title = "Time is up!"
content.badge = 1
content.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: "note1.wav"))
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timerDisplayed), repeats: false)
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
self.timerTextField.text = (" \(String(self.timerDisplayed))")
dismissKeyboard()
DispatchQueue.main.async {
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.Action), userInfo: nil, repeats: true)
}
}
@objc func Action(){
if timerDisplayed != 0 {
DispatchQueue.main.async {
self.timerDisplayed -= 1
self.timerTextField.text = (" \(String(self.timerDisplayed))")
}
}
else {
self.timer.invalidate()
self.timerTextField.text = nil
self.timerTextField.placeholder = " Timer"
}
}
也许您可以使用 timeClock()
方法,将其放在第一行:
let timerDisplayed = self.timerDisplayed > 0 ? timerDisplayed : Int(timeSelect[0])!
所以它可能看起来像这样:
@objc func timeClock(){
let timerDisplayed = self.timerDisplayed > 0 ? timerDisplayed : Int(timeSelect[0])!
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (didAllow, error) in }
let content = UNMutableNotificationContent()
content.title = "Time is up!"
content.badge = 1
content.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: "note1.wav"))
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timerDisplayed), repeats: false)
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
self.timerTextField.text = (" \(String(self.timerDisplayed))")
dismissKeyboard()
DispatchQueue.main.async {
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.Action), userInfo: nil, repeats: true)
}
}
或者您可以将其放入 viewDidLoad
以自动选择第一行:
let defaultFirstRow = pickerView.selectedRow(inComponent: 0);
pickerView(pickerView, didSelectRow: defaultFirstRow, inComponent:0)
除非我明确地 select 一行,否则 UIPickerView 崩溃并且我得到错误
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'time interval must be greater than 0'
我知道它崩溃是因为它没有 select 任何一行,并且时间间隔的默认值为 0。
那么我怎样才能让 PickerView
到 select 第一行,而不必自己明确 select 呢?
相关代码如下:
var timerDisplayed = 0
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
timerDisplayed = Int(timeSelect[row])!
}
@objc func timeClock(){
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (didAllow, error) in }
let content = UNMutableNotificationContent()
content.title = "Time is up!"
content.badge = 1
content.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: "note1.wav"))
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timerDisplayed), repeats: false)
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
self.timerTextField.text = (" \(String(self.timerDisplayed))")
dismissKeyboard()
DispatchQueue.main.async {
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.Action), userInfo: nil, repeats: true)
}
}
@objc func Action(){
if timerDisplayed != 0 {
DispatchQueue.main.async {
self.timerDisplayed -= 1
self.timerTextField.text = (" \(String(self.timerDisplayed))")
}
}
else {
self.timer.invalidate()
self.timerTextField.text = nil
self.timerTextField.placeholder = " Timer"
}
}
也许您可以使用 timeClock()
方法,将其放在第一行:
let timerDisplayed = self.timerDisplayed > 0 ? timerDisplayed : Int(timeSelect[0])!
所以它可能看起来像这样:
@objc func timeClock(){
let timerDisplayed = self.timerDisplayed > 0 ? timerDisplayed : Int(timeSelect[0])!
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (didAllow, error) in }
let content = UNMutableNotificationContent()
content.title = "Time is up!"
content.badge = 1
content.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: "note1.wav"))
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(timerDisplayed), repeats: false)
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
self.timerTextField.text = (" \(String(self.timerDisplayed))")
dismissKeyboard()
DispatchQueue.main.async {
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.Action), userInfo: nil, repeats: true)
}
}
或者您可以将其放入 viewDidLoad
以自动选择第一行:
let defaultFirstRow = pickerView.selectedRow(inComponent: 0);
pickerView(pickerView, didSelectRow: defaultFirstRow, inComponent:0)