Skip to content

Instantly share code, notes, and snippets.

@tkreuder
Created June 26, 2015 10:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tkreuder/754ee005e54464fde665 to your computer and use it in GitHub Desktop.
@interface ViewController ()
@property (nonatomic,strong) TestController *controller;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.controller=[[TestController alloc]init];
[self.controller addObserver:self forKeyPath:NSStringFromSelector(@selector(hasAllAccountsSelected)) options:NSKeyValueObservingOptionNew context:NULL];
}
-(void)dealloc
{
[self removeObserver:self forKeyPath:NSStringFromSelector(@selector(hasAllAccountsSelected))];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@",keyPath);
}
-(void)viewDidAppear:(BOOL)animated
{
[self.controller fooBar];
}
@end
@interface TestController : NSObject
@property (nonatomic,assign) BOOL hasAllAccountsSelected;
-(void)fooBar;
@end
@interface TestController ()
@property (nonatomic,strong)NSMutableSet *selectedAccounts;
@end
@implementation TestController
-(instancetype)init
{
self=[super init];
if(self)
{
_selectedAccounts=[NSMutableSet set];
}
return self;
}
+ (NSSet *)keyPathsForValuesAffectingHasAllAccountsSelected
{
return [NSSet setWithObjects:NSStringFromSelector(@selector(selectedAccounts)), nil];
}
-(BOOL)hasAllAccountsSelected
{
return self.selectedAccounts.count == [NSArray arrayWithObject:[[NSObject alloc]init]].count;
}
-(void)fooBar
{
[self.selectedAccounts addObject:[[NSObject alloc]init]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment