2017-07-17 34 views
-1

我希望我的用戶通知在用戶使用datePicker操作選擇通知顯示的日期和時間時顯示。使用DatePicker安排通知(swift3)

import UIKit 
import UserNotifications 

class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
     } 

@IBAction func datePicker(_ sender: Any) { 
} 
@IBAction func disaper(_ sender: Any) { 

    let c = UNMutableNotificationContent() 
    c.title = "Lets Roll" 
    c.subtitle = "dx" 
    c.body = "fyb" 

    let t = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) 
    let r = UNNotificationRequest(identifier: "any", content: c, trigger: t) 

    UNUserNotificationCenter.current().add(r, withCompletionHandler: nil) 

}} 
+0

好的,你想使用日期選擇器。進一步解釋你發佈的代碼與你陳述的目標有何關係。 –

+0

當datePicker從其初始日期發生更改時,我希望計劃通知,只要它是未來而不是過去的日期和時間。 –

+0

好的,你有什麼嘗試?您發佈的代碼應該做什麼,它不符合您的需求? –

回答

0

如果您想安排在確切日期將被下發的通知,你需要使用UNCalendarNotificationTrigger而不是UNTimeIntervalNotificationTrigger。您還應該設置一個出口到DatePicker,它使設置通知觸發器更容易。

let triggerDate = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: datePicker.date) 
let t = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: false) 
+0

如何聲明datePicker.date? –

+0

正如我在我的回答中提到的,您只需爲'DatePicker'設置一個'IBOutlet'。我認爲IBOutlet將被稱爲'datePicker'。 –

+0

是否有問題,但.date。有問題。錯誤消息指出「無法將類型'Date'的值轉換爲期望的參數類型'DateComponents'。 –