Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

ios - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...