【发布时间】:2015-08-07 17:05:37
【问题描述】:
我正在为我的应用程序使用 localNotifications。我有一个日期选择器,用户可以从中选择通知的日期和时间,而且我还有 3 个分段控件(每天、每周、每月)。我已经完成了 localnotification 的编码及其工作。但是分段控制不起作用,如果一个人选择两次,比如一次(下午 1 点)和第二次(下午 2 点),然后通知在两个时间都显示,这是我不想要的。下面是代码
在 appdelegate.swift 中
let types:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
let myAlarmSetting:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as Set<NSObject>)
UIApplication.sharedApplication().registerUserNotificationSettings(myAlarmSetting)
在notifications.swift中
@IBAction func NotificationButtonTapped(sender: AnyObject) {
func cancelLocalNotificationsWithUUID(uuid: String) {
for item in UIApplication.sharedApplication().scheduledLocalNotifications {
let notification = item as! UILocalNotification
if let notificationUUID = notification.userInfo?["UUID"] as? String {
if notificationUUID == uuid {
UIApplication.sharedApplication().cancelLocalNotification(notification)
}
}
}
}
var notifications:UILocalNotification = UILocalNotification()
notifications.fireDate = datePicker.date
notifications.timeZone = NSTimeZone.defaultTimeZone()
notifications.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
notifications.soundName = UILocalNotificationDefaultSoundName
notifications.userInfo = ["UUID": "YOUR NOTIFICATION ID"]
switch(frequencysegmentedcontrol.selectedSegmentIndex){
case 0:
notifications.repeatInterval = NSCalendarUnit.CalendarUnitDay
break;
case 1:
notifications.repeatInterval = NSCalendarUnit.CalendarUnitWeekOfYear
break;
case 2:
notifications.repeatInterval = NSCalendarUnit.CalendarUnitYear
break;
default:
notifications.repeatInterval = NSCalendarUnit(0)
break;
}
notifications.alertBody = "quote of the day"
notifications.category = "First_category"
UIApplication.sharedApplication().scheduleLocalNotification(notifications)
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
application.applicationIconBadgeNumber = 0
}
【问题讨论】:
标签: xcode swift datepicker