I had 2 Objects Obj1
and Obj2
. Where Obj1
is having a property isLocalChanges
(BOOL
) to check for any local changes made. I need to observe the change for this isLocalChanges
if there is any changes I am setting the value to YES
.
I written an observer for this property isLocalChanges
in Obj2
:
@interface Obj2 : NSObject
{
Obj1 *sampleObj;
}
@property (nonatomic,retain) Obj1 * sampleObj;
@end
@implementation Obj2
@synthesize sampleObj;
- (id)init
{
if (self = [super init])
{
sampleObj = [[Obj1 alloc] init];
[sampleObj addObserver:self
forKeyPath:@"isLocalChanges"
options:NSKeyValueObservingOptionNew
context:nil];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"isLocalChanges"]) {
NSLog(@"isLocalChanges Observer");
if ([[change objectForKey:@"new"] boolValue]) {
NSLog("@\n Found Local Changes...");
}
}
}
}
Declaration and usage of isLocalChanges property that I used:
@interface Obj1 : NSObject
{
BOOL _isLocalChanges;
}
@property (assign) BOOL isLocalChanges;
@end
and in the Obj1.m
did @synthesize isLocalChanges = _isLocalChanges;
And Inside the below method I am setting the value for isLocalChanges property.
-(void) localChangesMade
{
self.isLocalChanges = YES;
}
The issue is even if isLocalChanges
property changed in Obj1
, The method observeValueForKeyPath:
is not triggering. At this point I am helpless an unable to find what going wrong.
Can anyone suggest me to resolve this issue...