【问题标题】:Receiving UILocalNotification after adding it in Swift在 Swift 中添加后接收 UILocalNotification
【发布时间】:2016-03-21 03:50:34
【问题描述】:

我在提问之前进行了搜索,但一无所获。我正在使用此代码在我的应用中安排通知:

    let firstFormatter = NSDateFormatter()
    firstFormatter.dateFormat = "MMMM, dd, HH:mm"

    let date = "\(dueDateTxtField.text) \(notificationTimeTxtField.text)" 

    let fireDate = firstFormatter.dateFromString(date)

    let realNotification = UILocalNotification()
    realNotification.fireDate = fireDate
    realNotification.soundName = UILocalNotificationDefaultSoundName
    realNotification.alertTitle = "Homework reminder"
    realNotification.alertBody = "You have a pending \(subjectTxtField.text) homework for tomorrow."


    let secondFormatter = NSDateFormatter()
    secondFormatter.dateFormat = "yyyy"

    let falseNotification = UILocalNotification()
    falseNotification.fireDate = secondFormatter.dateFromString("2020")
    falseNotification.soundName = UILocalNotificationDefaultSoundName
    falseNotification.alertTitle = "This notification will never get to you"
    falseNotification.alertBody = "Never"

    if notifyMeSegmentedControl.selectedSegmentIndex == 0 {
        UIApplication.sharedApplication().scheduleLocalNotification(realNotification)
    } else {
        UIApplication.sharedApplication().scheduleLocalNotification(falseNotification)
    }

我正在使用 UISegmentedControl,因此用户可以选择接收或不接收通知。问题是,只要我添加一个新作业(该应用程序是一个家庭作业应用程序,您可以在其中添加主题、描述等),我已经收到通知,即使我将“notificationTimeTxtField”设置为现在不行。

请告诉我我做错了什么。 提前致谢!

编辑:我已尝试打印火灾日期,但打印为零!我不知道为什么,文本字段中有文本!

【问题讨论】:

  • 当您在控制台中打印出您的开火日期时,它是未来的日期吗?此外,对于用户不想收到通知的情况,可能希望使用 UIApplication.sharedApplication().cancelAllLocalNotifications()
  • 我已经试过了,它的打印为零!!!这怎么可能?
  • 您没有正确格式化字符串以转换日期。什么是 dueDateTextField 和 notificationTimeTextField ?它们的格式是否与您的日期格式完全一样
  • 不是真的,它们被格式化为其他格式

标签: swift uilocalnotification uisegmentedcontrol


【解决方案1】:

我修好了。问题是dueDateTxtField 的格式如下:“MMMM,dd,yyyy”,而我试图将其格式化为“MMMM,dd”。所以我通过用相同的格式格式化两者来修复它,它工作得很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    相关资源
    最近更新 更多