Quantcast
Channel: Active questions tagged uipickerview - Stack Overflow
Viewing all articles
Browse latest Browse all 592

App crashes when I select "done" button on UIPickerView before explicitly selecting a row

$
0
0

Unless I explicitly select a row, the UIPickerView crashes and I get the error

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'time interval must be greater than 0'

I understand that it crashes because it's not selecting any one of the rows, and the default value for Time Interval is 0.

So how can I get the PickerView to select the first row without me having to explicitly select it myself?

Here is the relevant code:

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

Viewing all articles
Browse latest Browse all 592

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>