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

Source: Internet
Author: User

Rootviewcontroller. m

-(Void) viewdidload {[Super viewdidload]; self. view. backgroundcolor = [uicolor greencolor]; // create a label uilabel * label = [[uilabel alloc] initwithframe: cgrectmake (50,100,200, 40)]; label. tag = 102; label. backgroundcolor = [uicolor graycolor]; [self. view addsubview: Label]; [label release]; // Add uibutton * button = [uibutton buttonwithtype: uibuttontyperoundedrect]; button. frame = cgrectmake (20, 20, 90, 60); [Button settitle: @ "enable mode" forstate: uicontrolstatenormal]; [Button addtarget: Self action: @ selector (buttonaction) forcontrolevents: uicontroleventtouchupinside]; [self. view addsubview: button];}-(void) viewwillappear :( bool) animated {uilabel * label = (uilabel *) [self. view viewwithtag: 102]; // create a singleton object backdata * backdata = [backdata upload data]; label. TEXT = backdata. text; [Super viewwillappear: animated];}-(void) buttonaction {modalviewcontroller * modalctrl = [[[modalviewcontroller alloc] init] autorelease]; modalctrl. modaltransitionstyle = uimodaltransitionstylefliphorizontal; [self presentviewcontroller: modalctrl animated: Yes completion: NULL];}
Modalviewcontroller. m

# Import "backdata. H "@ interface modalviewcontroller () @ end @ implementation modalviewcontroller-(ID) initwithnibname :( nsstring *) bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {// custom initialization} return self;}-(void) viewdidload {[Super viewdidload]; self. view. backgroundcolor = [uicolor redcolor]; // Add the uibutton * button = [uibutton buttonwithtype: uibuttontyperoundedrect]; button. frame = cgrectmake (20, 20, 90, 60); [Button settitle: @ "" forstate: uicontrolstatenormal]; [Button addtarget: Self action: @ selector (buttonaction) forcontrolevents: uicontroleventtouchupinside]; [self. view addsubview: button]; // create the input box uitextfield * textfield = [[uitextfield alloc] initwithframe: cgrectmake (50,100,200, 40)]; textfield. tag= 101; textfield. borderstyle = uitextborderstyleroundedrect; [self. view addsubview: textfield]; [textfield release];}-(void) buttonaction {uitextfield * field = (uitextfield *) [self. view viewwithtag: 101]; // create a singleton object backdata * Data = [backdata upload data]; data. TEXT = field. text; [self dismissviewcontrolleranimated: Yes completion: NULL];}

Backdata. h

@interface BackData : NSObject@property (nonatomic, copy) NSString *text;+ (BackData *)shareData;


Backdata. m

static BackData *data = nil;@implementation BackData+ (BackData *)shareData {    if (data == nil) {        data = [[BackData alloc] init];    }        return data;}




Singleton-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.