【问题标题】:App crashes when I select "done" button on UIPickerView before explicitly selecting a row当我在明确选择一行之前选择 UIPickerView 上的“完成”按钮时,应用程序崩溃
【发布时间】:2020-03-08 22:56:46
【问题描述】:

除非我明确选择一行,否则 UIPickerView 会崩溃并出现错误

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“时间间隔必须大于 0”

我知道它崩溃是因为它没有选择任何行,时间间隔的默认值为 0。

那么我怎样才能让PickerView 选择第一行而无需我自己明确选择呢?

以下是相关代码:

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"
        }
    }

【问题讨论】:

    标签: ios swift uipickerview


    【解决方案1】:

    也许你可以使用你的 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)
    

    【讨论】:

    • 我编辑了答案以显示一个工作示例,以在 viewdidload 中自动选择第一行作为默认值,或者通过使用 timerDisplayed 变量来调整您的代码以按照您希望的方式工作。请看一下:D
    • 是的,做到了!谢谢!
    猜你喜欢
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多