首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自datePicker的快速通知火灾

来自datePicker的快速通知火灾
EN

Stack Overflow用户
提问于 2015-12-13 00:19:36
回答 2查看 1K关注 0票数 3

我希望将我的fireDate的日期设置为本地通知datePicker。我发现了another answer at S.O.的代码

代码语言:javascript
复制
      @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.
 }

但是它不起作用,即使一切看起来都是correct...where的问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-13 08:31:38

就像这样:

代码语言:javascript
复制
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()
    }
}

您将需要这些扩展:

代码语言:javascript
复制
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
            )!
    }
}
票数 3
EN

Stack Overflow用户

发布于 2015-12-13 00:26:42

看起来,fireDate属性只在toggleSwitch()方法中设置。这种方法是如何达到的?我建议您将该方法的内容移到您的notificationsOptions()方法中。

顺便说一句,您可能需要考虑一些更有用的方法名称:datePicker()notificationOptions()可能会导致您以后的维护头疼,因为它们的描述性不强。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34246572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档