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?
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 assignrow
correctly.Instead of this
I included the line
interval = Double(row+1) * 60.0
into the function which hasmyLabel.text = pickerData[row]
, like this:This works and stores the correct value in
interval
, e.g. when you select the row "Every 2 minutes", it stores 120.0.You can simply add 1 to the selected row and then multiply by 60.0:
Create an array of minutes and declare your interval variable right under minutes array like so
Change your pickerView didSelectRow function to this
now you have your interval as int which is from 1 to 10 depending on which row has been selected.
I created a new minute variable which is a TimeInterval also multiply interval*60. If interval is 2 then it will be 2*60 = 120 and so on.