gpt4 book ai didi

iphone - 从 observeValueForKeyPath 调用方法。

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:32:05 24 4
gpt4 key购买 nike

这是我第一次使用 KVO,我马上就卡住了。问题是当调用 observeValueForKeyPath 时,我正在调用同一个类中的另一个方法。该方法只是显示一个警报 View 。我想的很简单,但警报 View 没有显示。

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

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

确保 UIAlertView 在主线程上创建。

关于iphone - 从 observeValueForKeyPath 调用方法。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11618876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com