How to use selected value of UIPickerView as time

2019-08-21 01:44发布

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?

3条回答
放荡不羁爱自由
2楼-- · 2019-08-21 02:25

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.

查看更多
地球回转人心会变
3楼-- · 2019-08-21 02:30

You can simply add 1 to the selected row and then multiply by 60.0:

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
}

@IBAction  func triggerNotification() {

    let content = UNMutableNotificationContent()
    content.title = "Placeholder"
    content.subtitle = "Placeholder"
    content.body = "Placeholder"
    content.sound = UNNotificationSound.default()

    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: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")
        }
    }
}
查看更多
贼婆χ
4楼-- · 2019-08-21 02:31

Create an array of minutes and declare your interval variable right under minutes array like so

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"]
let minutes[Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var interval: Inte = 0

Change your pickerView didSelectRow function to this

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {
    interval = minutes[row]
}

now you have your interval as int which is from 1 to 10 depending on which row has been selected.

@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 minute = TimeInterval(interval * 60)

    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval:(minute), 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")
        }
    }
}

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.

查看更多
登录 后发表回答