Iphone 从observeValueForKeyPath调用方法。

Iphone 从observeValueForKeyPath调用方法。,iphone,objective-c,ios,key-value-observing,Iphone,Objective C,Ios,Key Value Observing,这是我第一次使用KVO,我马上就被卡住了。问题是,当调用observeValueForKeyPath时,我正在调用同一类中的另一个方法。该方法只是显示一个警报视图。简单的事情我想,但警报视图没有显示 - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

这是我第一次使用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以外的任何其他方法调用它时,警报消息才会显示

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

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

确保在主线程上创建
UIAlertView

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