对于EKevent的addAlarm不能正常工作

时间:2015-09-27 16:41:30

标签: ios swift ekevent

我想在iPhone日历中注册,使用 EKevent addAlarm ,从存储到数组中的日期发出一些警报。

然而,结果只是注册了N的最后日期。

调试FOR循环,在变量 idEvent 中,我总能找到相同的id。

这些是警报管理的相关步骤:

let store = EKEventStore()
…
func PlanDays(sType: String) {
    //Calculates the days to schedule on the bases of the rules and records them  in the array.
    var dayToPlan = [NSDate]()
    …..
    // An example of adding an extra  date: 
    var dateformatter = NSDateFormatter()
    dateformatter.timeZone = NSTimeZone(name: “GMT”)
    dateformatter.dateStyle = NSDateFormatterStyle.MediumStyle
    dateformatter.locale = NSLocale(localeIdentifier: “it_IT”)
    var From: NSDate = dateformatter.dateFromString(txtdateFrom.text)!
    dayToPlan.append(From)
}
…
// Popup asking if I’ld like to proceed and if I do: 
let Procedo = UIAlertAction(title: “YES!”, style: .Default) { action -> Void in
    var ret: Bool = false
    var event = EKEvent(eventStore: self.store)
    var err: NSError?
    event.calendar = self.store.defaultCalendarForNewEvents
    for giorno in dayToPlan {
        event.startDate = giorno
        event.endDate = giorno
        event.title = self.txtTesto.text
        event.addAlarm(EKAlarm(absoluteDate: event.startDate))
        ret = self.store.saveEvent(event, span: EKSpanThisEvent, commit: true , error: &err)
        idEvento = event.eventIdentifier
    }
    ….
}

任何人都可以帮助我吗?

由于

1 个答案:

答案 0 :(得分:0)

您正在循环外创建EKEvent实例,这会导致一次又一次地重复使用同一个实例。如果你想要每天一个单独的事件/ al giorno在循环中创建一个新事件。

let Procedo = UIAlertAction(title: “YES!”, style: .Default) { action -> Void in
    var ret: Bool = false
    var err: NSError?
    event.calendar = self.store.defaultCalendarForNewEvents
    for giorno in dayToPlan {
        let event = EKEvent(eventStore: self.store)
        event.startDate = giorno
        event.endDate = giorno
        event.title = self.txtTesto.text
        event.addAlarm(EKAlarm(absoluteDate: event.startDate))
        ret = self.store.saveEvent(event, span: EKSpanThisEvent, commit: true , error: &err)
        idEvento = event.eventIdentifier
    }
    ….