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

标签 ios swift swift3 notifications uipickerview

我正在尝试从 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/

相关文章:

ios - 当与添加到 subview 的 UITableView 一起使用时,prefersLargeTitles 无法按预期工作

swift - 在 Swift 中实现自定义导航栏

ios - 如何在 Swift 中创建一个圆形按钮?

ios - 在 Swift 中将字符串拆分为数组?

objective-c - 从 CMSampleBuffer 制作 UIImage 会发生什么?

ios - 如何将自定义 UIView 转换到 Google Chromecast iOS?

swift - "Cannot inherit from non-open class" swift

ios - 从另一个文件访问方法时,scrollView setContentOffset 崩溃

ios - 如何定义向外设发送命令的正确数据格式?

objective-c - Xcode - 将东西上传到服务器/网络服务(如何?!)