从datePicker快速通知火 [英] Swift notification fire from datePicker

查看:106
本文介绍了从datePicker快速通知火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置为本地通知fireDate我的datePicker的日期。我发现 SO另一个答案的代码:

i want set as local notification fireDate the date of my datePicker. I found that code from 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.
 }

但它不起作用,即使一切似乎都正确...其中是问题吗?

But it doesn't work, even if all seems correct...where is the problem??

推荐答案

尝试这样:

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()
    }
}

您将需要这些扩展程序:

you will need those extensions:

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
            )!
    }
}

这篇关于从datePicker快速通知火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆