3
我有一个提醒应用程序,通过搜索数组发送提醒采取药物。当通知通过时,我得到了多个通知的海洋,似乎瞬间消失,然后1仍然在最后。Swift:本地通知似乎重复,然后一些消失
我的初始视图控制器是UITableViewController
。我试过将fireNotifications
函数放在viewDidLoad
中,但它似乎没有发送任何通知。如果我将fireNotifications
函数调用放在其中一个tableView函数中(即numberOfSectionsInTableView
),我可以获得通知,但它们有我在上面提到的奇怪行为。
我的代码是:
func fireNotifications() {
if NSUserDefaults().boolForKey("NotifySwitch") { //checks settings to make sure user wants reminders
for (index, items) in myMedsList.enumerate() {
let notification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: 20)
notification.alertBody = "Reminder: Take your \(items.name) now!"
notification.alertAction = "Let's take my meds!"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["Med Index": index]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
}
}
上午我做在我的函数调用的错误呢?或者代码中有什么错误?值得注意的是,我只有20秒的fireDate用于测试目的,但是计划为myMedsList数组中的每个项目定制fireDate。
添加药物时,为阵列每个项目安排一次更好吗?然后,我需要在每次编辑药物时编辑通知,这会更加复杂。用户可以编辑每种药物的每日时间,并且需要更改通知。因此,我在加载应用程序时让它们全部运行。将需要取消之前这样做,但这不应该改变我与测试有关的问题,因为他们都只会触发一次。 – LearningSwift
更新 - 你完全正确。我在运行for循环之前调用了它,并且现在他们正确地通过了。谢谢! – LearningSwift
我很乐意提供帮助! –