【问题标题】:Swift notification fire from datePicker来自 datePicker 的 Swift 通知触发
【发布时间】:2015-12-13 00:19:36
【问题描述】:

我想将我的 datePicker 的日期设置为本地通知 fireDate。我从another answer at S.O.找到了那个代码:

      @IBOutlet var myDatePicker: UIDatePicker!
      @IBOutlet var mySwitch: UISwitch!

var localNotification = UILocalNotification()   // You just need one
var notificationsCounter = 0

 // put your functions now
 func datePicker()            { myDatePicker.datePickerMode = UIDatePickerMode.Time }
func notificationsOptions()  {
localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.repeatInterval = .CalendarUnitDay
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
localNotification.alertAction = "Open App"
localNotification.alertBody = "Here is the seven o'clock notification"
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
//     you may add arbitrary key-value pairs to this dictionary.
//     However, the keys and values must be valid property-list types
//     if any are not, an exception is raised.
// localNotification.userInfo = [NSObject : AnyObject]?
}
  func toggleSwitch(){
if mySwitch.on{
    localNotification.fireDate = myDatePicker.date
} else {
    localNotification.fireDate = NSDate(timeIntervalSinceNow: 999999999999)          // will never be fired
}
  }
override func viewDidLoad() {
super.viewDidLoad()
datePicker()
notificationsOptions()
// Do any additional setup after loading the view, typically from a nib.
 }

但它不起作用,即使一切看起来都正确......问题出在哪里??

【问题讨论】:

    标签: swift datepicker localnotification


    【解决方案1】:

    试试这样:

    class ViewController: UIViewController {
    
        @IBOutlet var datePicker: UIDatePicker!
        @IBOutlet var notificationSwitch: UISwitch!
    
        let localNotification = UILocalNotification()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setUpNotificationsOptions()
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
    
        }
    
        func setUpNotificationsOptions()  {
            datePicker.datePickerMode = .Time
            localNotification.timeZone = NSTimeZone.localTimeZone()
            localNotification.repeatInterval = .Day
            localNotification.alertAction = "Open App"
            localNotification.alertBody = "a notification"
            localNotification.soundName = UILocalNotificationDefaultSoundName
        }
    
        func toggleNotification() {
            if notificationSwitch.on {
                localNotification.fireDate = datePicker.date.fireDate
                UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
            } else {
                localNotification.fireDate = nil
                UIApplication.sharedApplication().cancelLocalNotification(localNotification)
            }
        }
        @IBAction func toggleSwitch(sender: UISwitch) {
           toggleNotification()
        }
        @IBAction func dateChanged(sender: UIDatePicker) {
           toggleNotification()
        }
    }
    

    您将需要这些扩展:

    extension NSDate {
        var minute: Int {
            return NSCalendar.currentCalendar().component(.Minute, fromDate: self)
        }
        var hour: Int {
            return NSCalendar.currentCalendar().component(.Hour, fromDate: self)
        }
        var day: Int {
            return NSCalendar.currentCalendar().component(.Day, fromDate: self)
        }
        var month: Int {
            return NSCalendar.currentCalendar().component(.Month, fromDate: self)
        }
        var year: Int {
            return NSCalendar.currentCalendar().component(.Year, fromDate: self)
        }
        var fireDate: NSDate {
            let today = NSDate()
            return NSCalendar.currentCalendar().dateWithEra(1,
                year: today.year,
                month: today.month,
                day: { hour > today.hour || (hour  == today.hour
                   &&  minute > today.minute) ? today.day : today.day+1 }(),
                hour: hour,
                minute: minute,
                second: 0,
                nanosecond: 0
                )!
        }
    }
    

    【讨论】:

    • 谢谢你!!! “它只是工作”!!!!!!!你太棒了,你拯救了我的一天,我欠你的债!
    • 现在我有一个问题:所有代码都在“设置”控制器中......但我需要 localNotification.alertBody 值是主视图控制器中的数组的内容。如果我通过这个值,我会得到那个错误:使用未解析的标识符...我怎样才能通过这个限制?可以在设置控制器中声明一个新的 indexPath 吗?
    • @Swift1 随时打开另一个问题并在那里发布相关代码和解释,如果您想让我看一下,您可以在此处发布链接。
    • 好的,我在这里打开了一个新问题:stackoverflow.com/questions/34257339/… 请帮帮我!谢谢
    • 你知道如何将 datePicker 值存储到 NSuserdefaults 中吗?
    【解决方案2】:

    看起来fireDate 属性仅在toggleSwitch() 方法中设置。该方法是如何实现的?我建议您将该方法的内容直接移动到您的 notificationsOptions() 方法中。

    顺便说一句,您可能希望为您的方法考虑更有用的名称:datePicker()notificationOptions() 可能会在以后给您带来维护上的麻烦,因为它们的描述性不是很强。

    【讨论】:

      猜你喜欢
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多