2014-10-17 58 views
0

下面是我的問題的完整描述:我將日曆的事件提取一整天(即今天)並將它們存儲在一個數組中。如何從數組中隔離下一個相關(未傳遞的)事件的標題和時間,以將它們分別顯示爲標籤? 這裏是我的代碼:提前如何在變量中顯示事件的標題/時間以顯示在故事板上?

//Load Calendar Events 
    EKEventStore *store = [[EKEventStore alloc] init]; 

    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, 
                    NSError *error) { 
     if (granted) { 
     NSLog(@"User has granted permission"); 
     // Get the appropriate calendar 
     NSCalendar *calendar = [NSCalendar currentCalendar]; 

     // Create the start date components 
     NSDateComponents *beginDayComponents = [[NSDateComponents alloc] init]; 
     beginDayComponents.day = 0; 
     NSDate *todayStart = [calendar dateByAddingComponents:beginDayComponents 
                 toDate:[NSDate date] 
                options:0]; 

     // Create the end date components 
     NSDateComponents *endDayComponents = [[NSDateComponents alloc] init]; 
     endDayComponents.day = 0; 
     NSDate *todayEnd = [calendar dateByAddingComponents:endDayComponents 
                  toDate:[NSDate date] 
                  options:0]; 

     // Create the predicate from the event store's instance method 
     NSPredicate *predicate = [store predicateForEventsWithStartDate:todayStart 
                   endDate:todayEnd 
                   calendars:nil]; 

     // Fetch all events that match the predicate 
     NSArray *events = [store eventsMatchingPredicate:predicate]; 
     NSLog(@"Here are the events in the array, %@", events); 



     } else { 
      NSLog(@"User has not granted permission"); 
     } 
    }]; 

感謝,並有一個美好的一天!

回答

1

蘋果美國公司EKEventStore的文檔中,你將不得不先解決你的行列,從而使下一個待處理的事件是在索引0

注意:從日曆數據庫中檢索事件不 必須按時間順序返回事件。要按日期對EKEvent對象的數組進行排序,請在數組上調用sortedArrayUsingSelector: ,爲compareStartDateWithEvent:方法提供選擇器。

我建議你然後在數組的索引0處選擇EKEvent-Object並從中讀取屬性並將它們設置在您的標籤上。

EKEvent *event = [events objectAtIndex:0]; 
yourTitleLabel.text = event.text; 

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
formatter.dateformat = @"dd.MM HH:mm"; 
yourDateLabel.text = [formatter stringFromDate:event.startDate]; 

編輯: 你會排序的陣列事件這樣的:

events = [events sortedArrayUsingSelector:@selector(compareStartDateWithEvent:)]; 

對於這個工作,你必須輸入EventKit/EventKit.h

+0

謝謝!你能告訴我一個如何按日期對數組中的事件進行排序的例子嗎?我仍然是新的。但是,再次,謝謝! – 2014-10-17 22:00:01

+0

我已將它添加到我的答案中,請看一看。 – TMob 2014-10-20 10:14:35