如何使用UIPickerView的选定值作为通知的content.sound?

时间:2017-05-31 18:01:11

标签: ios swift notifications uipickerview

我一遍又一遍地阅读How to use selected value of UIPickerView as time interval for notifications?,但我不知道如何将答案应用于我的情况。

基本上,我已经用两个选项编写了一个选择器视图。我不知道如何

1)使每个选取器视图行对应一定的警报声
2)使content.sound成为一个变量,它可以是两个警报声音选项之一。

我的Assets.xcassets中已有两个声音文件,我只是不知道如何将其编入闹钟。

这是我到目前为止的代码:

@IBOutlet weak var soundPickerView: UIPickerView!

let sounds = ["chirping birds", "flowing water"]

func numberOfComponents(in pickerView: UIPickerView) -> Int
{
    return 1
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
    return sounds[row]
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
    return sounds.count
}


func scheduleNotification(at date: Date) {
    let calendar = Calendar(identifier: .gregorian)
    let components = calendar.dateComponents(in: .current, from: date)
    let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
    let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
    let content = UNMutableNotificationContent()
    content.title = " wake up call "
    content.body = "Time to wake up!"
    content.sound = UNNotificationSound(named: "Morning Birds.aiff")

还有其他代码用于通知请求等,但以上只是相关代码。最后一行代码是我知道需要更改的代码。

1 个答案:

答案 0 :(得分:0)

我认为你需要这样的东西:

class ViewController: UIViewController, UIPickerViewDelegate, 
        UIPickerViewDataSource {
        @IBOutlet weak var soundPickerView: UIPickerView!

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        let dic = [
             "chirping birds" : "MorningBirdsFile.aiff"
            ,"flowing water" : "FlowingWaterFile.aiff"
        ]

        func numberOfComponents(in pickerView: UIPickerView) -> Int
        {
            return 1
        }

        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
        {
            return Array(dic)[row].key
        }

        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
        {
            return dic.count
        }

        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            let calendar = Calendar(identifier: .gregorian)
            let components = calendar.dateComponents(in: .current, from: date)
            let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
            let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
            let content = UNMutableNotificationContent()
            content.title = " wake up call "
            content.body = "Time to wake up!"
            content.sound = UNNotificationSound(named: Array(dic)[row].value)


            print(Array(dic)[row].value)
        }
    }

或者您可以获取选择器视图的值并将其存储在某个变量中,当用户确认您将其设置为日历时。