【问题标题】:iOS: Interface not refreshingiOS:界面不刷新
【发布时间】:2016-05-16 23:33:26
【问题描述】:

在我的应用程序中,我创建了一个 EKCalendar。由于我的应用程序之外的用户可以删除日历,因此我需要检查该日历是否仍然存在,如下所示:

-(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"];
    }
}

我已经把这个方法放在了Application的Delegate中

- (void)applicationDidBecomeActive:(UIApplication *)application

我观察到的事情:

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

2) 但是:当它更新 UI 时,([self.LoginSwitch setOn:NO animated:NO];),什么也没有发生。

3) 当我重新运行项目(有效地强制退出并重新启动)时,用户界面实际上已更新。

我错过了什么?

具体问题是:为什么我的 UI 在应用运行时没有更新?

谢谢

【问题讨论】:

    标签: ios user-interface refresh


    【解决方案1】:

    我认为正在发生的事情是,我在本地日历中所做的更改并没有通过 iCloud 同步系统完全扩散。或者类似的东西。不确定。 但是:为了解决我的问题,我添加了

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

    到我的应用程序。

    -(void)storeChanged:(NSNotification *) notification {
    
        [self initCalendarState];
    }
    

    这完美无缺。

    【讨论】:

      猜你喜欢
      • 2021-08-19
      • 1970-01-01
      • 2012-09-05
      • 2011-01-08
      • 2023-02-08
      • 1970-01-01
      • 2013-04-20
      • 2011-12-22
      • 1970-01-01
      相关资源
      最近更新 更多