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]