2011-05-29 65 views
1

我有一個應用程序以編程方式創建日曆事件,它正在工作,我只是讓它檢索用戶日曆並在拾取器中顯示它們,但我有內存泄漏。你可以看到它,因爲我試圖釋放一切......另外我的主要問題是如何保存到用戶選擇的日曆,[事件setCalendar:calendararray];不起作用。 calendararray是一個EKCalendar * calendararray,我將用戶選定的日曆設置爲它。爲什麼這不起作用?我如何使它工作...查找iOS日曆

日曆.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); 

回答

3

EKEventStore有calendars屬性,它是EKCalendar實例的一個NSArray。然後,您可以獲取每個EKCalendar的標題並將其全部顯示給用戶,以便他們可以選擇他們希望添加新事件的日曆。

+0

好吧,告訴我如何得到某處,我發現代碼顯示它在日誌中,但我環顧四周,找不到如何將其轉換爲字符串我發佈了上面的代碼。感謝所有的幫助。 – BDGapps 2011-05-30 03:05:08

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

但你一次只給我一次卡,所以我不能把它放在桌子上嗎? – BDGapps 2011-05-30 16:48:08

+0

您必須在for循環中執行此操作。 – 2011-05-31 11:23:42

+0

請看看我上面的更新。謝謝 – BDGapps 2011-06-01 15:44:15

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];  
} 
+0

它保存在「日曆」中,而不是我選擇的工作之一 – BDGapps 2011-06-04 16:08:30

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

可以NSLog的使用FOR循環迭代 「指標」 使用此代碼下面,知道你想要哪一個。

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

希望這有助於

+0

你試過這個嗎? – 2011-06-07 00:14:49

+0

是的,我已經嘗試了上述我得到它與來自全國各地的工作,但你可以幫助我與我得到的錯誤。謝謝http://stackoverflow.com/questions/6352689/ekcalendar-error – BDGapps 2011-06-15 16:26:36

+0

當然,我會看看!如果你有它的工作,請通過選擇最合適的答案來解決這個問題,綠色的勾號。 – 2011-06-15 18:36:55

1

我不知道,如果你發佈的代碼是相同的版本,但是如果你把它們一起我會看到至少存在以下問題:

你說

[event setCalendar:calendararray]; 

不起作用。

看起來calendararray ist設置在eventview.mcalendararray:方法。

這又被稱爲calendar.mpickerView:didSelectRow:,其中它從arrayColors中選擇對象。

calendar.mviewDidLoad方法中,arrayColors用用戶的日曆標題進行初始化,而不是用日曆進行初始化。 所以你最終給EKEventsetCalendar:方法NSString而不是EKCalendar

不知道你的程序的其餘部分,作爲一個修復,我會盡量保持EKCalendar對象本身在arrayColors

所以,在calendar.m嘗試將

[arrayColors addObject:thisCalendar.title]; 
viewDidLoad

改變

[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變化calendararray:

- (void) calendararray:(EKCalendar *)array{ 
NSLog(@"calendarNameTextFieldStringFromTable %@", array); 

calendararray = array; 
calendarLabel.text = array.title; 
calendarLabel.textColor = [UIColor brownColor]; 

} 
+0

關於更一般的說明: *您編譯時是否收到任何警告?你應該和他們應該幫助你找到問題 – 2011-06-05 10:58:49

+1

*請不要編輯你的問題是一個完全不同的問題(即「我如何找到所有用戶的日曆?」與「爲事件設置特定的日曆不爲我工作「);反而開一個新的問題;否則,人們爲第一個版本的問題提供的答案對於只能看到 – 2011-06-05 10:59:44

+0

*問題的最新版本的新讀者看起來是無意義的* calendararray:方法在內存管理方面看起來很可疑;該數組應該可能被保留(看看[內存管理編程指南](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html),特別是部分在[訪問器方法]上(http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAccessorMethods.html))。 – 2011-06-05 10:59:54