0

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

Ariel
  • 2,430
  • 1
  • 17
  • 20
Irrd
  • 325
  • 1
  • 6
  • 18

2 Answers2

1

From the code above I think you want to do one to one communication. If thats the case go for KVO and not NSNotification.

Replace the code like this,

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

        [self addObserver:self forKeyPath:@"frame" options: (NSKeyValueObservingOptionNew |NSKeyValueObservingOptionInitial) context:nil];
        isObserving=true;
    }
}
-(void)ignoreFrameChanges
{
    if (isObserving) {
        [self 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];
    }
}
CatVsDogs
  • 342
  • 1
  • 11
0

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.

prabodhprakash
  • 3,825
  • 24
  • 48