Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 使用日期选择器的本地通知_Xcode_Swift_Datepicker - Fatal编程技术网

Xcode 使用日期选择器的本地通知

Xcode 使用日期选择器的本地通知,xcode,swift,datepicker,Xcode,Swift,Datepicker,我正在为我的应用程序使用localNotifications。我有一个日期选择器,用户可以从中为通知选择日期和时间,我还有3个分段控件(每日、每周、每月)。我已经为localnotification及其工作进行了编码。但是分段控件不工作,如果一个人选择了两次,那么还有一个问题(下午1点)和第二(下午2点),然后通知在这两个时间显示,我不想。下面是代码 在appdelegate.swift中 let types:UIUserNotificationType = UIUserNotifica

我正在为我的应用程序使用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)

这是因为您在计划新通知时没有取消旧通知,因此您可以始终向通知中添加信息,如下所示:

var notification = UILocalNotification()
...
notification.userInfo = ["UUID": "YOUR NOTIFICATION ID"]
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 notification = UILocalNotification()
...
notification.userInfo = ["UUID": "YOUR NOTIFICATION ID"]
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)
            }    
        }
    }
}
以下是完整的代码:

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

@IBAction func NotificationButtonTapped(sender: AnyObject) {

    cancelLocalNotificationsWithUUID("NotificationID")

    var notifications:UILocalNotification = UILocalNotification()
    notifications.fireDate = datePicker.date
    notifications.timeZone = NSTimeZone.defaultTimeZone()
    notifications.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
    notifications.soundName = UILocalNotificationDefaultSoundName

    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.userInfo = ["UUID": "NotificationID"]

    notifications.alertBody = "quote of the day"
    notifications.category = "First_category"

    UIApplication.sharedApplication().scheduleLocalNotification(notifications)
}

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    application.applicationIconBadgeNumber = 0
}

谢谢你,我的代码中的notifications.repeatinterval=NSCalendarunit.Calendarunitday行中也有这样的内容,你能纠正一下吗?我应该在iAction中包含func cancellocalnotification…吗@Firas@shivam事实上,我不理解代码背后的目标,你应该在任何时候调用它来取消之前的notif是的,如果你只有一个通知,并且它应该在固定的基础上重复,那么你应该在你的操作中调用它,如果它有效,请接受答案。@shivam如果它有效,请接受答案,或者至少接受upvote。嘿,我是通知新手,所以我不知道在哪里发布此代码。我必须在我的IBAction中发布此代码吗@菲拉斯