gpt4 book ai didi

ios - 如何使用 UIPickerView 的选定值作为通知的时间间隔?

转载 作者:行者123 更新时间:2023-11-30 12:33:05 25 4
gpt4 key购买 nike

我正在尝试从 UIPickerView 中的所选行中获取一个值,并根据所选行设置重复通知的时间间隔。即,用户在 UIPickerView 中选择“每 2 分钟”并每 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])
}
}

有人可以帮我解决这个问题吗?

最佳答案

创建一个分钟数组并在分钟数组下声明间隔变量,如下所示

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,依此类推。

关于ios - 如何使用 UIPickerView 的选定值作为通知的时间间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43217476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com