2011-10-12 45 views
4

我用下面的代碼創建一個事件,顯示彈出要求用戶保存事件報警的日曆事件:創建使用EKEventEditViewController

EKEventStore *eventStore = [[EKEventStore alloc] init]; 
EKCalendar *calendar = [eventStore defaultCalendarForNewEvents]; 
EKEvent *event = [EKEvent eventWithEventStore:eventStore]; 
event.calendar = calendar; 
event.title = [NSString stringWithFormat:@"Event: %@", [self.event title]]; 
event.location = self.event.location; 
event.notes = [self stringByStrippingHTML: [self.event description]]; 
event.startDate = [self.event startDate]; 
event.endDate = [self.event endDate]; 

NSTimeInterval alarmOffset = -1*60*60;//1 hour 
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:alarmOffset]; 

[event addAlarm:alarm]; 
EKEventEditViewController *eventViewController = [[EKEventEditViewController alloc] init]; 
eventViewController.event = event; 
eventViewController.eventStore=eventStore; 
eventViewController.editViewDelegate = self; 
[self.navigationController presentModalViewController:eventViewController animated:YES]; 

這工作得很好,除了事件報警財產不被設置,你可以看到形成下面的圖片:

Alarm Not Set

如果我展示它得到報警設置視圖控制器之前保存的事件。

請注意我正在使用LLVM編譯器,所以不要擔心沒有發佈的東西!

羅斯

+0

後,你有沒有解決這個問題後,上面的代碼?我不認爲這是可能的。我認爲最好的解決辦法是創建自己的editEventController – Herman

回答

4

終於找到了如何做到這一點。

您的控制器必須實現協議UINavigationControllerDelegate,並將EKEventEditViewController委託設置爲self。然後,只需執行

navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

方法並添加報警那裏。

這是我的實現。

- (void)navigationController:(UINavigationController *)navigationController 
    willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { 

    if ([navigationController isKindOfClass:[EKEventEditViewController class]]) { 
     EKEventEditViewController *ek = (EKEventEditViewController*)navigationController; 
     EKEvent *event = ek.event; 
     // set alarm to 15 mins prior of the event if it starts later than 15 mins out 
     if ([event.startDate compare:[[NSDate date] dateByAddingTimeInterval:60*15]] != NSOrderedAscending) { 
     EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:60*15*-1]; 
     event.alarms = [NSArray arrayWithObject:alarm]; 
     }  
    } 
} 
4
NSTimeInterval alarmOffset = -1*60*60;//1 hour 
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:alarmOffset]; 

[event addAlarm:alarm]; 

寫呈現控制器即行

[self.navigationController presentModalViewController:eventViewController animated:YES]; 
+0

它是如何產生影響的? – Satyam