2016-02-07 44 views
0

在我的应用程序中,我创建了一个EKCalendar。由于日历可以通过我的应用程序之外的用户被删除,我需要检查,如果该日历仍然存在,就像这样:iOS:界面不刷新

-(BOOL)checkForCalendar { 

    NSArray *calendarArray = [self.store calendarsForEntityType:EKEntityTypeEvent]; 


    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSString *calNameToCheckFor = [defaults objectForKey:@"calendarName"]; 

    EKCalendar *cal; 

    for (int x = 0; x < [calendarArray count]; x++) { 

     cal = [calendarArray objectAtIndex:x]; 
     NSString *calTitle = [cal title]; 

     // if the calendar is found, return YES 
     if ([calTitle isEqualToString:calNameToCheckFor]) { 

      return YES; 
     } 
    } 
    return NO; 

} 

我想我的UI更新,如果一个日历确实被删除,因为这样的:

-(void)initCalendarState { 
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
    if (![self checkForCalendar]){ 
     [self.LoginSwitch setOn:NO animated:NO]; 
     [userDefaults setObject:@"0" forKey:@"SwitchedOn"]; 
    } 
} 

我已经把在应用程序的委托这种方法

- (void)applicationDidBecomeActive:(UIApplication *)application 

事情我观察:

1)如果我在调试器中一步一步地通过这段代码,它会在正确的时刻被调用,并按预期执行:在主线程中运行,遵守所有条件等。

2)但是:when它会更新UI,([self.LoginSwitch setOn:NO animated:NO];),没有任何反应。

3)当我重新运行项目(有效强制退出并重新启动)时,UI实际上会更新。

我错过了什么?

具体的问题是:为什么不在应用程序运行时更新我的​​UI?

感谢提前

回答

0

我认为正在发生的事情,是我在本地日历所做的更改不是通过icloud的同步系统完全激增。或者类似的东西。不确定。 但是:解决我的问题,我补充

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(storeChanged:) 
               name:EKEventStoreChangedNotification 
               object:self.store]; 

到我的应用程序。

-(void)storeChanged:(NSNotification *) notification { 

    [self initCalendarState]; 
} 

这完美的作品。