Single case transfer value ------- if there are many different pages, you need to pass the value, save the value to a third party, set the third party to the singleton Mode
The Code is as follows:
<pre name="code" class="objc">#import <Foundation/Foundation.h>@interface Singleton : NSObject+ (Singleton *)shareSingleton;@property (nonatomic,copy)NSString * text;@end
#import "Singleton.h"@implementation Singletonstatic Singleton * singleton = nil;+ (Singleton *)shareSingleton{ @synchronized(self){ if (singleton == nil) { singleton = [[Singleton alloc]init]; } } return singleton;}@end
# Import "firstviewcontroller. H "# import" secondviewcontroller. H "# import" uibutton + create. H "# import" Singleton. H "@ interface firstviewcontroller () {uitextfield * _ textfield; // create an input box} @ end @ implementation firstviewcontroller-(void) dealloc {[_ textfield release]; [Super dealloc];}-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: callback]; If (Self) {// initim initialization} return self;}-(void) viewdidload {[Super viewdidload]; self. view. backgroundcolor = [uicolor redcolor]; self. navigationitem. title = @ "Homepage";/*** 1. create an input box **/_ textfield = [[uitextfield alloc] initwithframe: cgrectmake (50, 80,200, 30)]; _ textfield. borderstyle = uitextborderstyleroundedrect; [self. view addsubview: _ textfield];/*** 1. create a uibutton, * 2. add a response event to jump from the home page to the second page. */uibutton * button = [uibutton systembuttonwithframe: cgrectmake (100,120, 50, 50) Title: @ "push" target: Self action: @ selector (didclickbuttonaction)]; [self. view addsubview: button]; // do any additional setup after loading the view .} -(void) didclickbuttonaction {/*** 1. use the push method to launch the next page * 2. input the string in the homepage input box to receive the string in the singleton mode. in this way, the strings entered in the homepage input box are uploaded to the uilabel on the second page. */secondviewcontroller * secondvc = [[secondviewcontroller alloc] init]; [Singleton sharesingleton]. TEXT = _ textfield. text; [self. navigationcontroller pushviewcontroller: secondvc animated: Yes]; [secondvc release];}-(void) didreceivemorywarning {[Super didreceivemorywarning]; // dispose of any resources that can be recreated .} @ end
# Import "secondviewcontroller. H "# import" Singleton. H "@ interface secondviewcontroller () @ end @ implementation secondviewcontroller-(void) dealloc {[_ label release]; [Super dealloc];}-(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 orangecolor]; self. navigationitem. title = @ "second page";/*** 1. create a uilabel * 2 in the second interface. receives the string entered in the homepage input box through the single-instance class attribute nsstring * text * 3. then assign a value to uilabel */_ label = [[uilabel alloc] initwithframe: cgrectmake (50, 80,200, 30)]; _ label. backgroundcolor = [uicolor greencolor]; _ label. TEXT = [Singleton sharesingleton]. text; [self. view addsubview: _ label]; // do any additional setup after loading the view .} -(void) didreceivememorywarning {[Super didreceivemorywarning]; // dispose of any resources that can be recreated .} @ end
Single-case data transfer