#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property BOOL myBoolean;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myBoolean;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myBoolean = false;
[self addObserver:self forKeyPath:@"myBoolean" options:NSKeyValueObservingOptionNew context:nil];
myBoolean = true;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
if([keyPath isEqual:@"myBoolean"]){
NSLog(@"changed detected");
}
}
-(void)viewDidDisappear:(BOOL)animated{
[self removeObserver:self forKeyPath:@"myBoolean"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Here I am trying to do a simple value change checking with KVO. I wasn't sure what to put in the forKeyPath so I put the variable name of "myBoolean."
I set up the boolean to be false before then add the observer, then make the boolean to be true. It does not give me the NSLog for "changed detected"
What is the proper way of using KVO?