KVO --- data transmission between views: labels display input content [in multiple views]

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.