Search code examples
iosswiftswift3notificationsuipickerview

How to use selected value of UIPickerView as time interval for notifications?


I'm trying get a value out of the selected row in UIPickerView and, depending on the selected row, set a time interval for repeating notifications. i.e., users choose "Every 2 minutes" in UIPickerView and get a notification every 120.0 seconds.

The second "didSelectRow" method does not seem to store the value in my variable interval, I didn't find a solution for that so far.

This is my code so far:

import UIKit
import  UserNotifications
import UserNotificationsUI

class ViewController: UIViewcontroller, UIPickerViewDataSource, UIPickerViewDelegate {
    // The following lines define the parameters for Picker View
    @IBOutlet weak var myLabel: UILabel!
    @IBOutlet weak var myPicker: UIPickerView!

    let pickerData = ["Every minute", "Every 2 minutes", "Every 3 minutes", "Every 4 minutes", "Every 5 minutes", "Every 6 minutes", "Every 7 minutes", "Every 8 minutes", "Every 9 minutes", "Every 10 minutes"]

    override func viewDidLoad() {
        super.viewDidLoad()
        myPicker.delegate = self
        myPicker.dataSource = self
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return pickerData.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return pickerData[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        myLabel.text = pickerData[row]
    }

    var interval: TimeInterval = 60.0 // Assume that row 0 is selected by default

    // The following method might be the tricky part I guess.

    func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {
interval = Double(row+1) * 60.0
    }


    let requestIdentifier = "SampleRequest" //identifier is to cancel the notification request

    @IBAction  func triggerNotification(){
        print("notification will be triggered in five seconds..Hold on tight")
        let content = UNMutableNotificationContent()
        content.title = "Placeholder"
        content.subtitle = "Placeholder"
        content.body = "Placeholder"
        content.sound = UNNotificationSound.default()

        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval:self.interval, repeats: true)
        let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)

        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().add(request){(error) in
            if (error != nil){
                print(error?.localizedDescription ?? "User instance is nil")
            }
        }
    }

    @IBAction func stopNotification(_ sender: AnyObject) {

        print("Removed all pending notifications")
        let center = UNUserNotificationCenter.current()
        center.removePendingNotificationRequests(withIdentifiers: [requestIdentifier])
    }
}

Can anyone help me with this?


Solution

  • Seems that I've solved it by doing the following. Since I noticed that everything is running correctly until the point where I'm trying to store my needed value in interval - the method just wouldn't do it, but the method before would assign row correctly.

    Instead of this

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int,          inComponent component: Int) {
        myLabel.text = pickerData[row]
    }
    
    var interval: TimeInterval = 60.0 // Assume that row 0 is selected by default
    
    func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {
    interval = Double(row+1) * 60.0
    }
    

    I included the line interval = Double(row+1) * 60.0 into the function which has myLabel.text = pickerData[row], like this:

    var interval: TimeInterval = 60.0 // Assume that row 0 is selected by default
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int,          inComponent component: Int) {
        myLabel.text = pickerData[row]
        interval = Double(row+1) * 60.0
    }
    

    This works and stores the correct value in interval, e.g. when you select the row "Every 2 minutes", it stores 120.0.