查找iOS日历

时间:2011-05-29 17:04:03

标签: iphone objective-c xcode ipad ios4

我有一个应用程序以编程方式创建日历事件它正在工作,我刚刚创建它,以便它检索用户日历并在选择器中显示它们但我有内存泄漏。你能看到它吗,因为我尝试释放所有东西......另外我的主要问题是如何将它保存到用户选择的日历中,[event setCalendar:calendararray];不起作用。 calendararray是一个EKCalendar * calendararray,我正在设置用户选择的日历。为什么这不起作用????我如何让它发挥作用......

calendar .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 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

NSLog(@"Calendar %lu Title = %@", (unsigned long)counter, thisCalendar.title);
NSString *title = [NSString stringWithFormat:@"Calendar %lu Title = %@", (unsigned long)counter, thisCalendar.title];
NSLog(title);

答案 2 :(得分:1)

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

你说

[event setCalendar:calendararray];

不起作用。

似乎在calendararray的{​​{1}}方法中设置eventview.m

这反过来从calendararray:的{​​{1}}调用,从calendar.m获取所选对象。

pickerView:didSelectRow:的{​​{1}}方法中,使用用户的日历标题初始化arrayColors,而不是日历。 因此,您最终将calendar.m的{​​{1}}方法设为viewDidLoad而不是arrayColors

我不知道你的其他程序,作为修复,我会尝试将EKEvent个对象保留在setCalendar:中。

所以,在NSString尝试更改

EKCalendar

EKCalendar

arrayColors

然后将calendar.m更改为

    [arrayColors addObject:thisCalendar.title];

最后,在viewDidLoad更改 [arrayColors addObject:thisCalendar];

pickerView:titleForRow:

答案 3 :(得分:0)

尝试这种方式:

#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];       
}

答案 4 :(得分:0)

好的,我知道你想要什么,继续我的方式:

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

希望这有帮助