简体   繁体   English

完成后如何设置默认日历?

[英]How can I set my default calendar when done is pressed?

I want the user to be able to chose a new default calendar with the EKCalendarChooser. 我希望用户能够使用EKCalendarChooser选择新的默认日历。 So far this is what I thought that might work but it is not. 到目前为止,这是我认为可能有效的方法,但并非如此。

   func calendarChooserDidFinish(_ calendarChooser: EKCalendarChooser) {
    print("Done was pressed")
    // Set to default calendar
    eventStore.defaultCalendarForNewReminders()
    for source in eventStore.sources {
        if source.sourceType == .local {
            calendar.source = source
            break
        }
    }
    dismiss(animated: true, completion: nil)
}

How can I set a new default calendar with the EKCalendarChooser? 如何使用EKCalendarChooser设置新的默认日历?

 //get your calendar if it was previously created
    let calendars = eventStore.calendars(for: .event)
    for c in calendars
    {
        if c.title == "calendarName"
        {
            calendar = c
        }
    }

//create it if it was not created previously
    if calendar == nil
    {
        let calendar = EKCalendar(for: .event, eventStore: eventStore)
        calendar.title = "calendarName"
        if eventStore.sources.count == 0
        {
            calendar.source = EKSource()
        }
        else
        {
            self.setStoreForCalendar(calendar: calendar, store: eventStore)
        }
        do
        {
            try eventStore.saveCalendar(calendar, commit: true)
        }
        catch let err as NSError
        {
            print ("error  \(err.description)")
        }
    }

//set defaultCalendar 
    func setStoreForCalendar(calendar:EKCalendar, store:EKEventStore)
    {
        var mSource:EKSource?
        for source in store.sources
        {
            if source.sourceType == .calDAV && source.title == "iCloud"
            {
                mSource = source
            }
        }

        if mSource == nil
        {
            // saving on the local calendar
            mSource = store.defaultCalendarForNewEvents?.source
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在日历中设置默认提醒? - How to set a default alert in the calendar? 我可以设置UITextField来在按下Return键时触发UIBarButtonItem的操作吗 - Can I set a UITextField to trigger the action of a UIBarButtonItem when Return is pressed 按下完成后无法停止在MPMoviePlayerController中发生动画 - Can't stop animation occurring in MPMoviePlayerController when done is pressed 如何判断 NSData dataWithContentsOfFile 何时完成加载我的 plist? 我什么时候可以获取/分配值? - How can I tell when NSData dataWithContentsOfFile is done loading my plist? When can I fetch/assign values? 如何在按下按钮时播放音频,而当我将手指从同一按钮上移开时如何立即停止播放? - How can I make audio play when the button is pressed and immediately stop when I take my finger off the same button? 完成后如何从MPMoviePlayerController中删除动画 - how to remove animation from the MPMoviePlayerController when done is pressed 如何检测长按退格键的时间? - How can I detect when the Backspace key is long pressed? 按下导航后退按钮时如何调用功能? - How can I call a function when the navigation back button is pressed? 按下时如何使textView坐在键盘上方? - How can I make a textView sit on top of the keyboard when pressed? 快速按下按钮时如何翻转整个Tableview? - How can I flip an entire Tableview when a button is pressed in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM