如何使用 UIPickerView 的选定值作为通知的时间间隔? [英] How to use selected value of UIPickerView as time interval for notifications?
问题描述
我正在尝试从 UIPickerView 中的所选行中获取一个值,并根据所选行设置重复通知的时间间隔.即用户在 UIPickerView 中选择每 2 分钟"并每 120.0 秒收到一次通知.
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.
第二个didSelectRow"方法似乎没有将值存储在我的变量 interval
中,到目前为止我还没有找到解决方案.
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.
这是我目前的代码:
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?
推荐答案
似乎我已经通过执行以下操作解决了它.因为我注意到一切都在正确运行,直到我尝试在 interval
中存储我需要的值 - 该方法不会这样做,但之前的方法会分配 row
正确.
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.
代替这个
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
}
我将行 interval = Double(row+1) * 60.0
包含到具有 myLabel.text = pickerData[row]
的函数中,如下所示:>
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
}
这可以工作并在 interval
中存储正确的值,例如当您选择每 2 分钟"行时,它会存储 120.0.0.0.
This works and stores the correct value in interval
, e.g. when you select the row "Every 2 minutes", it stores 120.0.
这篇关于如何使用 UIPickerView 的选定值作为通知的时间间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!