我尝试从UIPickerView中的所选行中获取一个值,并根据所选行设置重复通知的时间间隔。即,用户选择"每2分钟"在UIPickerView中,每120.0秒收到一次通知。
第二个" didSelectRow"方法似乎没有将值存储在我的变量interval
中,到目前为止我还没有找到解决方案。
到目前为止,这是我的代码:
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])
}
}
任何人都可以帮我吗?
答案 0 :(得分:0)
创建一个分钟数组,并在minles数组下声明你的间隔变量,如此
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
将pickerView didSelectRow函数更改为此
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {
interval = minutes[row]
}
现在你的间隔为int,取决于选择了哪一行,从1到10。
@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")
}
}
}
我创建了一个新的分钟变量,它是一个TimeInterval,也是乘法间隔* 60。如果间隔是2那么它将是2 * 60 = 120,依此类推。
答案 1 :(得分:0)
您可以简单地将1添加到所选行,然后乘以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")
}
}
}
答案 2 :(得分:0)
似乎我通过执行以下操作解决了这个问题。因为我注意到一切都正常运行,直到我试图在interval
中存储我需要的值 - 该方法不会这样做,但之前的方法会正确地分配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
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]
的函数中,如下所示:
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。