【问题标题】:call methods from observeValueForKeyPath.从 observeValueForKeyPath 调用方法。
【发布时间】:2012-07-23 19:07:56
【问题描述】:

这是我第一次使用 KVO,我马上就卡住了。问题是,当调用 observeValueForKeyPath 时,我正在调用同一个类中的另一个方法。该方法只是显示一个警报视图。我认为简单的事情,但警报视图不显示。

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
                     change:(NSDictionary *)change context:(void *)context
{
    [self beginUpdate];
}


 -(void)beginUpdate
 {
     NSLog(@"Check!");
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"message" message:@"Hi" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
     [alert show];
 } 

显示日志消息。仅当我从observeValueForKeyPath 以外的任何其他方法调用它时,才会显示警报消息。

【问题讨论】:

    标签: iphone objective-c ios key-value-observing


    【解决方案1】:

    据我所知,observeValueForKeyPath: 是在修改观察对象的线程上下文中调用的。另一方面,对 UI 的更改只能在主线程上完成。试试

    dispatch_async(dispatch_get_main_queue(), ^{
        [self beginUpdate];
    });
    

    [self performSelectorOnMainThread:@selector(beginUpdate) withObject:nil waitUntilDone:NO]
    

    确保在主线程上创建UIAlertView

    【讨论】:

      猜你喜欢
      • 2011-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多