iphone - Add Observer to BOOL variable -
is possible add observers simple variables such bools or nsintegers , see when change?
thanks!
you observe keys notified when value changes. data type can anything. defined objective-c property (with @property in .h file) ready go if want observe bool property add view controller follows:
in myviewcontroller.h:
@interface myviewcontroller : uiviewcontroller { bool mysetting; } @property (nonatomic) bool mysetting; in myviewcontroller.m
@implementation myviewcontroller @synthesize mysetting; // rest of myviewcontroller implementation @end in otherviewcontroller.m:
// assumes myvc defined property of otherviewcontroller - (void)presentmyviewcontroller { self.myvc = [[[myviewcontroller alloc] init] autorelease]; // note: remove self observer before myvc released/dealloced [self.myvc addobserver:self forkeypath:@"mysetting" options:0 context:nil]; // present myvc modally or navigation controller here } - (void)observevalueforkeypath:(nsstring *)keypath ofobject:(id)object change:(nsdictionary *)change context:(void *)context { if (object == self.myvc && [keypath isequaltostring:@"mysetting"]) { nslog(@"othervc: value of self.myvc.mysetting has changed"); } }
Comments
Post a Comment