Search code examples
iosuitableviewnsnotificationcenteraddobserver

exception says sent to an object that is not KVC-compliant for the "frame" property


Inside my custom UITableViewCell I am doing something like this.

-(void)checkHeight
{
    if (self.frame.size.height < self.expandedHeight) {
        self.lblReasontitle.hidden=YES;
    }
    else
    {
        self.lblReasontitle.hidden=NO;
    }
}
-(void)watchFrameChanges
{
    if (!isObserving) {

        [[NSNotificationCenter defaultCenter] addObserver:self forKeyPath:@"frame" options: (NSKeyValueObservingOptionNew |NSKeyValueObservingOptionInitial) context:nil];
        isObserving=true;
    }
}
-(void)ignoreFrameChanges
{
    if (isObserving) {
        [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"frame"];
    }
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"frame"]) {
        [self checkHeight];
    }
}

But I am getting this exception.

Terminating app due to uncaught exception NSUnknownKeyException, reason: [ addObserver: forKeyPath:@"frame" options:5 context:0x0] was sent to an object that is not KVC-compliant for the "frame" property.

I have no idea whats that exception and how can I solve it. Please helpme. Thanks


Solution

  • UIKit elements are not KVO compliant, by default - until documented. Thus, you are getting this exception. Try registering for some value related to frame and it should work.