5

Using accessors seems to be the way to get KVO notifications on collection objects, but this doesn't appear to work on the childViewControllers NSArray property of a UIViewController. I want to be notified when an object is added or removed to this property. So I tried this:

[self addObserver:self forKeyPath:@"childViewControllers" options:NSKeyValueObservingOptionNew context:nil];

-(NSUInteger)countOfChildViewControllers
{
    return self.childViewControllers.count;
}

But I'm not sure if I'm doing it right, I'm not getting any notifications. Is what I want possible?

Snowman
  • 31,411
  • 46
  • 180
  • 303

1 Answers1

0

The point is that all modifications of the property have to go through KVO-compliant means. This has to do with how UIViewController is implemented, nothing you can do from outside of that class. Using the indexed collection accessors is one way that the implementation of UIViewController could achieve KVO-compliance, but there are others. I don't know if that property is KVO-compliant; from what you report, apparently not. Adding getters or even setters in a category or subclass doesn't help if the underlying implementation doesn't use them.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154