Lable in the second interface displays the text in the first interface TextField
First we set up a rootviewcontrollers and a detailviewcontrollers, declare a TextString property in Detailviewcontrollers, to receive the passed string.
Also create a lable to display the passed string
Introduce detailviewcontrollers on Rootviewcontrollers and declare a TextField property to enter the string
Then on rootviewcontrollers we create and add a button that responds to the corresponding method when the button is clicked to switch between views to complete the values between views
(ii) Block transfer value
The value of the block is also passed from the first interface to the second interface.
First we are in the detailviewcontrollers. h file, the properties
In the rootviewcontrollers. m file, the other is unchanged, and in the response method of the button we complete the block pass value for the Block property assignment
(iii) Proxy value
Rootviewcontrollers page push to Detailviewcontrollers page, if detailviewcontrollers page information want to return (callback) to Rootviewcontrollers page, The value is passed by proxy, where Detailviewcontrollers defines the protocol and claims agent, Rootviewcontrollers confirms and implements the proxy, Rootviewcontrollers as Agent of Detailviewcontrollers
First we create the Protocol method in the DetailViewControllers.h file
In Detailviewcontrollers. m when we determine that the proxy object exists, bind the method to it
Rootviewcontrollers. m files We specify the agent and let it execute the proxy method
(iv) Single-case pass-through value
Single-pass value (for sharing)
APPSTATUS.H Create a singleton class Appstatus
1 #import <Foundation/Foundation.h> 2 3 @interface appstatus:nsobject 4 {5 nsstring *_contextstr; 6} 7
8 @property (nonatomic,retain) NSString *contextstr; 9 + (Appstatus *) shareinstance;11 @end
Appstatus.m
1 #import "AppStatus.h" 2 3 @implementation appstatus 4 5 @synthesize contextstr = _contextstr; 6 7 Static App Status *_instance = nil; 8 9 + (Appstatus *) ShareInstance10 {each if (_instance = = nil) { _instance = [[Super alloc]init];14< c12/>}15 return _instance;16}17-(ID) init19 {(Self = [Super init]) {$ }24 return self;25}26-(void) dealloc28 { [super dealloc];30}31 @end
RootViewController.h
1 #import "RootViewController.h" 2 #import "DetailViewController.h" 3 #import "AppStatus.h" 4 5 @interface Rootviewcontr Oller () 6 7 @end 8 9 @implementation RootViewController10-(void) LoadView12 {13//core code UIButton *BTN = [U IButton buttonwithtype:uibuttontyperoundedrect];15 btn.frame = CGRectMake (0, 0, +); [btn settitle:@ "Push" forstate:0];17 [btn addtarget:self Action: @selector (pushaction:) forcontrolevents:uicontroleventtouchupinside];18 [Self.view addsubview:btn];19}20-(void) Pushaction: (id) sender22 {% TF = (Uitextfield *) [Self.view viewwithtag:100 0];24 25//single-pass value of the information to be transmitted into a single case (shared) [[Appstatus Shareinstance]setcontextstr:tf.text]; This is equivalent to the following notation [appstatus shareinstance].contextstr = tf.text;28//navigation push to the next page//pushviewcontroller into the stack reference count +1 , and control system Detailviewcontroller *detailviewcontroller = [[Detailviewcontroller alloc]init];31 32//NAV push to next page 33 [Self.navigationcontroller Pushviewcontroller:deTailviewcontroller animated:yes];34 [Detailviewcontroller release];35} Notoginseng @end
DetailViewController.h
1 #import <uikit/uikit.h>2 @protocol changedelegate;//Notification compiler has this agent 3 4 @interface Detailviewcontroller: UIVIEWCONTROLLER5 {6 uitextfield *textfield;7}8 9 @end
Detailviewcontroller.m
1 #import "DetailViewController.h" 2 #import "AppStatus.h" 3 4 @interface Detailviewcontroller () 5 6 @end 7 8 @implem Entation Detailviewcontroller 9 @synthesize navititle = _navititle;11-(void) loadView13 {self.view = [[Uivie W alloc]initwithframe:cgrectmake (0, 0, 480)]autorelease];15 16//Singleton self.title = [Appstatus shareinstance]. contextstr;18 TextField = [[Uitextfield alloc]initwithframe:cgrectmake]];19 textfield.borderstyl E = uitextborderstyleline;20 [Self.view addsubview:textfield];21 [TextField release];22 at Uibarbuttonitem *do Neitem = [[Uibarbuttonitem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action: @selector (doneaction:)];24 self.navigationItem.rightBarButtonItem = doneitem;25 [Doneitem release];26}27 28//This method is performed multiple times Equivalent to flush view29-(void) Viewwillappear: (BOOL) animated30 {[Super viewwillappear:animated];32 TF = (Uitextfield *) [sel F.view viewwithtag:1000];33 Tf.text = [Appstatus shareinstance].contextstr;34}35//pop back to previous page PNS-(void) Doneaction: (id) sender38 {39//Singleton Pass value 40 [Appstatus shareinstance].contextstr = textfield.text;41 [Self.navigationcontroller popToRootViewControllerAnimated : yes];42}
(v) Notification of the value of the transfer
Who wants to listen to the change of value, who will register notice in particular, notice that the recipient of the notification must exist for this prerequisite
A page RootViewController.h
1 #import <uikit/uikit.h>2 #import "DetailViewController.h" 3 @interface Rootviewcontroller:uiviewcontroller <changedelegate>4 {5
A page rootviewcontroller.m
1 #import "IndexViewController.h" 2 #import "DetailViewController.h" 3 #import "AppStatus.h" 4 5 @implementation INDEXVI Ewcontroller 6 7-(void) Dealloc 8 {9 [[Nsnotificationcenter Defaultcenter] Removeobserver:self10 name:@ "Change_title" object:nil];11 [Super dealloc];12}13-(ID) init15 {if (self = [super init]) [[Nsnotificationcenter Defaultcenter] Addobserver:self19 Selector: @selector (change:) name:@ "change_t Itle "object:nil];22}23 return self;24}25-(void) Change: (Nsnotification *) aNoti27 {28//Notification transfer value Nsdictionary *dic = [Anoti userinfo];30 nsstring *str = [dic valuefork ey:@ "Info"];31 uitextfield *TF = (Uitextfield *) [self.view viewwithtag:1000];33 tf.text = str;34}35 36- (void) Viewwillappear: (BOOL) animated37 {[Super viewwillappear:animated];39/*40//single-pass value of Uitextfield *TF = (Uitextfield *) [sel F.view viewwithtag:1000];42 tf.text = [appstatus shareinstance].contextstr;43 */44}45 @end
DetailViewController.h
1 #import <uikit/uikit.h>2 @protocol changedelegate;//Notification compiler has this agent 3 4 @interface Detailviewcontroller: UIVIEWCONTROLLER5 {6 uitextfield *textfield;7}8 @end
Detailviewcontroller.m
1 #import "DetailViewController.h" 2 #import "AppStatus.h" 3 4 @implementation detailviewcontroller 5 @synthesize Nav Ititle = _navititle; 6 7-(void) Loadview 8 {9 Uibarbuttonitem *doneitem = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem: Uibarbuttonsystemitemdonetarget:self Action: @selector (doneaction:)];10 Self.navigationItem.rightBarButtonItem = doneitem;11 [doneitem release];12}13//Pop back to previous page-(void) doneaction :(ID) sender16 {nsdictionary *dic = [nsdictionary dictionaryWithObject:textField.text forkey:@ "Info"];18 19 [[ Nsnotificationcenter Defaultcenter] postnotificationname:@ "Change_title" Object:nil userInfo:dic];20 21 [ Self.navigationcontroller popviewcontrolleranimated:yes];22 23}
iOS pass-through: properties, proxies, block, Singleton, notifications