ios - Updating uilabel in view controller underneath another -
i have 2 view controllers, firstviewcontroller , fourthviewcontroller. firstviewcontroller initial view controller. present fourthviewcontroller with
uiviewcontroller *fourthcontroller = [self.storyboard instantiateviewcontrollerwithid:@"fourth"]; [self presentviewcontroller:fourthcontroller animated:yes completion:nil];
then, in fourthviewcontroller's .m i'd change text of uilabel in firstviewcontroller. use
uiviewcontroller *firstcontroller = [self.storyboard instantiateviewcontrollerwithid:@"first"]; firstcontroller.mainlab.text = [nsmutablestring stringwithformat:@"new text"];
however, after use
[self dismissviewcontrolleranimated:yes completion:nil];
i find mainlab's text has not updated. know why?
when calling line fourthviewcontroller.m creating new instance of firstviewcontroller, rather using created one.
uiviewcontroller *firstcontroller = [self.storyboard instantiateviewcontrollerwithid:@"first"];
you can tackle in 2 ways.
1) using notification
post notification fourthviewcontroller when label text need changed.
[[nsnotificationcenter defaultcenter] postnotificationname:@"updatelabel" object:self];
in firstviewcontroller viewdidload
methodcreate observer waits notification fired.
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(updatelabelcalled:) name:@"updatelabel" object:nil];
implement updatelabelcalled:
, update label.
- (void) updatelabelcalled:(nsnotification *) notification { if ([[notification name] isequaltostring:@"updatelabel"]){ //write code update label } }
2) implementing delegate
it explained here in stackoverflow. basic idea create fourthviewcontroller delegate, , create delegate method updatelabel. firstviewcontroller should implement method.
Comments
Post a Comment