代码之家  ›  专栏  ›  技术社区  ›  Community wiki

查找iOS日历

  •  1
  • Community wiki  · 技术社区  · 2 年前

    我有一个应用程序,它以编程方式创建日历事件,它正在工作,我只是制作了它,以便它检索用户日历并在选择器中显示它们,但我有内存泄漏。你能看到吗,因为我试着释放一切。。。此外,我的主要问题是如何将其保存到用户选择的日历中[事件集日历:日历阵列];不起作用。日历数组是EKCalendar*日历数组,我正在将用户选择的日历设置为它。为什么不起作用????我如何使它工作。。。

    日历m

     #import "calendar.h"
    
    
     @implementation calendar
     @synthesize delegate;
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
    [super viewDidLoad];
    
    
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    
    /* These are the calendar types an iOS Device can have. Please note
     that the "type" property of an object of type EKCalendar
     is of type EKCalendarType. The values in the "CalendarTypes"
     array reflect the exact same values in the EKCalendarType
     enumeration, but as NSString values */
    NSArray *calendarTypes = [NSArray arrayWithObjects:
                              @"Local",
                              @"CalDAV",
                              @"Exchange",
                              @"Subscription",
                              @"Birthday",
                              nil];
    
    /* Go through the calendars one by one */
    NSUInteger counter = 1;
    for (EKCalendar *thisCalendar in eventStore.calendars){
    
        /* The title of the calendar */
        NSLog(@"Calendar %lu Title = %@", 
              (unsigned long)counter, thisCalendar.title);
    
    5 回复  |  直到 14 年前
        1
  •  3
  •   smountcastle    14 年前

    EKEventStore有一个 calendars 属性,该属性是EKCalendar实例的NSArray。然后,您可以获取每个EKCalendar的标题,并将所有标题显示给用户,以便用户选择要将新事件添加到的日历。

        2
  •  1
  •   BoltClock    14 年前
    NSLog(@"Calendar %lu Title = %@", (unsigned long)counter, thisCalendar.title);
    NSString *title = [NSString stringWithFormat:@"Calendar %lu Title = %@", (unsigned long)counter, thisCalendar.title];
    NSLog(title);
    
        3
  •  1
  •   Jörn Eyrich    14 年前

    我不确定你发布的所有代码是否都来自同一版本,但如果你把它们放在一起,我至少会看到以下问题:

    你说

    [event setCalendar:calendararray];
    

    不起作用。

    看起来 calendararray 已设置 eventview.m s calendararray: 方法

    依次从调用 calendar.m s pickerView:didSelectRow: ,从中获取选定对象 arrayColors .

    在里面 日历m s viewDidLoad 方法 阵列颜色 是用用户日历的标题而不是日历初始化的。 所以你最终放弃了 EKEvent s setCalendar: 方法a NSString 而不是 EKCalendar .

    不知道你程序的其余部分,作为一种修复方法,我会尽量保留 EKCalendar 对象本身在 阵列颜色 .

    所以,在 日历m 尝试更改

        [arrayColors addObject:thisCalendar.title];
    

    在里面 查看DidLoad

        [arrayColors addObject:thisCalendar];
    

    然后更改 pickerView:titleForRow:

    - (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
       EKEvent *event = [arrayColors objectAtIndex:row]
       return event.title;
    }
    

    最后,在 eventview。m 改变 日历阵列:

    - (void) calendararray:(EKCalendar *)array{
    NSLog(@"calendarNameTextFieldStringFromTable %@", array);
    
    calendararray = array;
    calendarLabel.text = array.title;
    calendarLabel.textColor = [UIColor brownColor];
    
    }
    
        4
  •  0
  •   marzapower Vijay-Apple-Dev.blogspot.com    14 年前

    试试这个方法:

    #import "EventTestViewController.h"
    
    #import "EventKit/EventKit.h"
    
    @implementation EventTestViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        EKEventStore *eventStore = [[EKEventStore alloc] init];
    
        EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
        event.title     = @"EVENT TITLE";
    
        event.startDate = [[NSDate alloc] init];
        event.endDate   = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
    
        [event setCalendar:[eventStore defaultCalendarForNewEvents]];
        NSError *err;
        [eventStore saveEvent:event span:EKSpanThisEvent error:&err];       
    }
    
        5
  •  0
  •   Nicolas S    14 年前

    好吧,我明白你想要什么了,我的方式是:

    EKEventStore *eventStore = [[EKEventStore alloc] init];
    
    NSArray *calendarArray = [eventStore calendars]; //Here you get all the device calendars
    
    EKEvent *event = [EKEvent eventWithEventStore:eventStore];
    event.title = @"Event Title";
    
    event.startDate = [[NSDate alloc] init]; event.endDate = [[NSDate alloc] initWithTimeInterval:816 sinceDate:event.startDate];
    
    [event setCalendar:[calendarArray objectAtIndex:1]]; //here you set which calendar you want.. 
    NSError *err;
    [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
    

    您可以使用下面的代码使用FOR循环迭代“索引”来NSLog,以知道您想要哪个。

     NSLog(@"Cal name> %@. Index of cal> %i", [[calendarArray objectAtIndex:index] title], index);
    

    希望这能有所帮助