IOS, UIView, Detect Hidden State Change in Subview

风流意气都作罢 提交于 2019-11-30 02:08:06

问题


Is there anyway to detect a hidden state change (or other change) in a sub view in a UIView (not UIViewController). Would like to detect this async somehow.

There are reasons for my madness.


回答1:


You can use KVO (key value observing) to detect a change to the value of the property hidden.

Add your observer (self in this example) in the following way:

UIView* viewToObserve = [self getViewToObserve];  // implement getViewToObserve
[viewToObserve addObserver:self forKeyPath:@"hidden" options:0 context:NULL];

Now add the following method to your observer class:

- (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
  UIView* viewToObserve = [self getViewToObserve];
  if (object == viewToObserve)
  {
    if ([keyPath isEqualToString:@"hidden"])
    {
      // react to state change
    }
  }
}

The observer method will be invoked whenever the hiddenproperty changes its value. If I am not mistaken, the method will be invoked synchronously in the context of the thread that makes the change to the property. If you need asynchronous notification you can add that yourself, for instance by using one of the NSObject methods performSelector:withObject:afterDelay: or performSelector:onThread:withObject:waitUntilDone:.

BTW: You don't need the checks in the observer method, obviously, if you only observe a single object and/or property. I left the checks in for illustration purposes. I also recommend reading Apple's documentation on KVO and KVC (key value coding) to understand what's going on here.

The runtime happily continues notifying your observer even if the observer is deallocated - resulting in an application crash! So don't forget to remove the observer before it is de-allocated, at the latest this should happen in the observer's dealloc:

- (void) dealloc
{
    UIView* viewToObserve = [self getViewToObserve];
    [viewToObserve removeObserver:self forKeyPath:@"hidden"];
    [super dealloc];
}


来源:https://stackoverflow.com/questions/17033581/ios-uiview-detect-hidden-state-change-in-subview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!