UIApplication的“windows”值上的KVO?

Ari*_*lka 4 iphone uikit

以下不起作用:

[[UIApplication sharedApplication] addObserver:self forKeyPath:@"windows"
   options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
   context:NULL];
Run Code Online (Sandbox Code Playgroud)

除此之外,在观察者方面:

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
  NSLog(@"never reached!");
}
Run Code Online (Sandbox Code Playgroud)

有什么线索吗?

注意我的超级目标是在显示(系统生成的) UIAlertView 时收到通知。

Ari*_*lka 5

自我回答...

检测何时显示任意 UIAlertView 的正确方法是使用 NSNotificationCenter:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeVisible:) name:UIWindowDidBecomeVisibleNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

然后,在里面:

- (void) windowDidBecomeVisible:(NSNotification*)notification {}
Run Code Online (Sandbox Code Playgroud)

检查有问题的 UIWindow(可通过 notification.object 访问)是否包含作为 UIAlertView 实例的子视图