ios - KVO observeValueForKeyPath method called twice -
i write demo wanted trying kvo programming,i found observevalueforkeypath method execute twice when change value once,what's wrong?please me,thanks
code
statussyncer.h
#import <foundation/foundation.h> @interface statussyncer : nsobject @property nsstring *title; +(instancetype)sharedinstance; @end
statussyncer.m
#import "statussyncer.h" @implementation statussyncer + (instancetype)sharedinstance { static id _sharedinstance = nil; static dispatch_once_t oncepredicate; dispatch_once(&oncepredicate, ^{ _sharedinstance = [[self alloc] init]; }); return _sharedinstance; } @end
viewcontroller.m
- (void)viewdidload { [super viewdidload]; [self.statussyncer addobserver:self forkeypath:@"title" options:nskeyvalueobservingoptionnew context:nil]; } -(void)observevalueforkeypath:(nsstring *)keypath ofobject:(id)object change:(nsdictionary *)change context:(void *)context{ if ([keypath isequaltostring:@"title"]) { nslog(@"chane"); } } -(statussyncer *)statussyncer{ if (_statussyncer==nil) { _statussyncer = [statussyncer sharedinstance]; } return _statussyncer; }
detailviewcontroller.m
- (ibaction)changeaction:(id)sender { self.statussyncer.title= @"newtitleć¶"; } -(statussyncer *)statussyncer{ if (_statussyncer==nil) { _statussyncer = [statussyncer sharedinstance]; } return _statussyncer; }
output:
2015-06-15 12:58:49.849 kvoadvence[2433:74541] chane 2015-06-15 12:58:49.850 kvoadvence[2433:74541] chane
i think method addobserver: performed twice.
you check other controllers inheritance, may extends viewcontroller
Comments
Post a Comment