ios - How to observe the property change if a particular obj using KVO? -
i have 2 controller namely aviewcontroller
, bviewcontroller
. have textfield in bviewcontroller
(called txt1
). have declared following:
in bviewcontroller.m:
- (void)viewdidload { [self addobserver:self forkeypath:@"txt1.text" options:nskeyvalueobservingoptionnew context:nil]; }
and in aviewcontroller.m:
- (void)viewdidload { bviewcontroller *obj = [[bviewcontroller alloc] init]; [obj addobserver:self forkeypath:@"txt1.text" options:nskeyvalueobservingoptionnew context:null]; [super viewdidload]; // additional setup after loading view, typically nib. } -(void)observevalueforkeypath:(nsstring *)keypath ofobject:(id)object change:(nsdictionary *)change context:(void *)context { /* nslog(@"%@",keypath); nslog(@"%@",object); nslog(@"%@",change); */ nslog(@"%s",__pretty_function__); if ([keypath isequaltostring:@"txt1.text"]) { nslog(@"text1 content changed"); } }
when add text in txt1(after tapping return key), getting error following:
terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: '<bviewcontroller: 0x9134560>: -observevalueforkeypath:ofobject:change:context: message received not handled. key path: txt1.text observed object: <bviewcontroller: 0x9134560> change: { kind = 1; new = werwetw; } context: 0x0' *** first throw call stack: (0x1c92012 0x10cfe7e 0x1c91deb 0xb85406 0xb2267d 0xb2233c 0xb09417 0xb22b24 0xad7d60 0xb21eb5 0xdd707 0xe4b02 0xedda1 0xdc645 0x121fb5 0x1220e1 0xdc4e6 0x2a0a 0xe4d2b 0xed9f8 0x1923cf 0x198f7f 0x198a8c 0x1979fe 0x1a1c72 0x24ddb 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x24e35 0x24806 0x24beb 0x16698 0x1beddf9 0x1bedad0 0x1c07bf5 0x1c07962 0x1c38bb6 0x1c37f44 0x1c37e1b 0x1bec7e3 0x1bec668 0x13ffc 0x1fa2 0x1ed5) libc++abi.dylib: terminate called throwing exception
can tell me went wrong.
a couple of observations:
b's
viewdidload
adding observer own property. that's unnecessary.i wonder if b has implemented
observevalueforkeypath
. that's moot because of prior point, if add observer, must implementobservevalueforkeypath
. strikes me source of exception.a creating local instance of b, adding observer, , if you're using arc, you're letting b fall out of scope, being released, , have observer object no longer exists. or if you're not using arc, won't have problem, you'll leak copy of b.
you you're getting b , changing text, have assume in attempt simplify code sample, omitted code prevent problem manifesting (e.g. code pushes/presents b), perhaps not issue. key point here before item has observers released, make sure observers removed.
unrelated problem, b's
viewdidload
isn't calling[super viewdidload]
.you're showing creation of observers, not removing of observers @ appropriate time.
as general counsel, in mvc environments ios, wouldn't suggest adding observers view (i.e.
uitextfield
). i'd have b update model properties based upon changes in view, , have observer model property, not in b's view hierarchy. important in ios versions prior 6.0, because if b subsequently invoked third controller, c, , low memory condition arose, b's view released (and don't want observers on items deallocated).
it strikes me before go further diagnosing kvo, should demonstrate how invoking b.
is controller segues to? in case shouldn't add observer until that.
is b child controller , a custom container? in case have add custom container calls (e.g.
addchildcontroller
, etc.).
if can articulate logical flow between , b, can better you. or perhaps more complete code snippets help. perhaps articulate business problem you're trying solve.
having said of that, if you're trying pass data between view controllers, there far more effective ways of doing (e.g. delegate protocols). kvo isn't right technology if you're trying pass data view controller controller presented it.
Comments
Post a Comment