Rootviewcontroller. m
# Import "modalviewcontroller. H "@ interface rootviewcontroller () @ end @ implementation rootviewcontroller {modalviewcontroller * modalctrl;}-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) else {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {// custom initialization} return self;}-(void) viewdidload {[Super viewdidload]; self. view. backgroundcolor = [uicolor redcolor]; uilabel * textlabel = [[uilabel alloc] initwithframe: cgrectmake (50,100,200, 20)]; textlabel. tag = 100; textlabel. backgroundcolor = [uicolor orangecolor]; [self. view addsubview: textlabel]; uibutton * button = [uibutton buttonwithtype: uibuttontyperoundedrect]; button. frame = cgrectmake (50,150,100, 30); [Button settitle: @ "open" forstate: uicontrolstatenormal]; [Button addtarget: Self action: @ selector (buttonaction) forcontrolevents: uicontroleventtouchupinside]; [self. view addsubview: button]; modalctrl = [[modalviewcontroller alloc] init]; // listen to the text attribute of the modal controller [modalctrl addobserver: Self forkeypath: @ "text" Options: nskeyvalueobservingoptionnew context: null];} // KVO trigger method-(void) observevalueforkeypath :( nsstring *) keypath ofobject :( ID) object change :( nsdictionary *) change context :( void *) context {If ([keypath isw.tostring: @ "text"]) {nsstring * text = [Change objectforkey: @ "new"]; uilabel * label = (uilabel *) [self. view viewwithtag: 100]; label. TEXT = text ;}}- (void) buttonaction {[self presentviewcontroller: modalctrl animated: Yes completion: NULL];}
Modalviewcontroller. m
@ Interface modalviewcontroller () {nsstring * _ text;} @ end @ implementation modalviewcontroller-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {// custom initialization} return self;}-(void) viewdidload {[Super viewdidload]; self. view. backgroundcolor = [uicolor greencolor]; uitextfield * textfiled = [[uitextfield alloc] initwithframe: cgrectmake (50, 60,160, 30)]; textfiled. tag = 100; textfiled. delegate = self; textfiled. borderstyle = uitextborderstyleroundedrect; [self. view addsubview: textfiled]; // display the keyboard [textfiled becomefirstresponder]; uibutton * button = [uibutton buttonwithtype: uibuttontyperoundedrect]; button. frame = cgrectmake (50,150,100, 30); [Button settitle: @ "back" forstate: uicontrolstatenormal]; [Button addtarget: Self action: @ selector (buttonaction) forcontrolevents: uicontroleventtouchupinside]; [self. view addsubview: button] ;}// click the event-(void) buttonaction {[self dismissviewcontrolleranimated: Yes completion: NULL]; // uitextfield * field = (uitextfield *) [self. view viewwithtag: 100]; // nsstring * text = field. text; // self. TEXT = text ;}# Pragma uitextfield delegate // click return to call the Protocol method-(bool) textfieldshouldreturn :( uitextfield *) textfield {// collapse the keyboard [textfield resignfirstresponder]; return yes;}-(bool) textfield :( uitextfield *) textfield shouldchangecharactersinrange :( nsange) range replacementstring :( nsstring *) string {nslog (@ "string: % @", string ); // string is the last nslog (@ "% @", textfield. text); // textfield. text is the first character of the input content (not included in the last character) // uitextfield * field = (uitextfield *) [self. view viewwithtag: 100]; // nsstring * text = field. text; nsstring * STR = [nsstring stringwithformat: @ "% @", textfield. text, string]; nslog (@ "str: % @", STR); self. TEXT = STR; return yes ;}
KVO --- data transmission between views: labels display input content [in multiple views]