Original Http://www.cnblogs.com/496668219long/p/4471757.html Example: Lable in the second interface shows the text in the first interface TextField
This requires the use of attribute values, block values
So the first view controller how to get a second view of the controller part of the information
For example, the lable in the first interface displays the text in the second interface TextField
This will require the use of proxy values
There are eight ways to pass values between pages, let's briefly introduce several ways to transfer values between pages:
(i) attribute passing value
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 second interface to the first 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-pass value single-pass value (realize sharing)
APPSTATUS.H Create a singleton class Appstatus
2 *) shareinstance; @end
Appstatus.m
1#import "AppStatus.h"23@implementationAppstatus45@synthesize contextstr = _contextstr;67 staticAppstatus *_instance =Nil89 + (Appstatus *) shareinstance10 {11if (_instance = =Nil12 {_instance = [[super alloc]init]; 14} 15 return _instance; }17 18-(ID) init19 {20 if (self = [super init]) 21 {22 23}24 return self; 25} 26 27-(void) Dealloc28 {29 [ span class= "keyword" >super dealloc]; 30} 31 32 @end
RootViewController.h
1 #Import"RootViewController.h"2 #Import"DetailViewController.h"3 #Import"AppStatus.h"45@interface Rootviewcontroller ()67@end89@implementation Rootviewcontroller1011-(void) Loadview12 {13Core codeUIButton *btn = [UIButton buttonwithtype:uibuttontyperoundedrect];Btn.frame = CGRectMake (0,0,100,30);[Btn settitle:@"Push" forstate:0];[Btn addtarget:self Action:@selector (pushaction:) forcontrolevents:uicontroleventtouchupinside];[Self.view ADDSUBVIEW:BTN];19}2021-(void) Pushaction: (ID) sender22 {TF = (Uitextfield *) [Self.view Viewwithtag:1000];2425 //single-pass value the information to be delivered is stored in the singleton (shared) 26 //[[Appstatus Shareinstance]setcontextstr:tf.text]; This is equivalent to the following notation 27 [ Appstatus shareinstance].contextstr = Tf.text; 28 //navigation push to next page 29 // Pushviewcontroller into the stack reference count +1, and control to the system 30 detailviewcontroller *detailviewcontroller = [[ Detailviewcontroller Alloc]init]; 31 32 //navigation push to next page 33 [ Self.navigationcontroller Pushviewcontroller:detailviewcontroller Animated:yes]; 34 [Detailviewcontroller release]; 35} 36 37 @end
DetailViewController.h
#import <UIKit/UIKit.h>changedelegate; Notifies the compiler that there is this agent Uiviewcontroller5 {Uitextfield *textfield; 7}@end
Detailviewcontroller.m
1 #Import"DetailViewController.h"2 #Import"AppStatus.h"34@interface Detailviewcontroller ()56@end78@implementation Detailviewcontroller910@synthesize navititle = _navititle;1112-(void) Loadview13 {Self.view = [[[UIView Alloc]initwithframe:cgrectmake (0,0,320,480)]autorelease];1516Single caseSelf.title = [Appstatus shareinstance].contextstr;TextField = [[Uitextfield alloc]initwithframe:cgrectmake (100,100,150,30)];Textfield.borderstyle = Uitextborderstyleline;[Self.view Addsubview:textfield];[TextField release];22Uibarbuttonitem *doneitem = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemdonetarget: Self action:@selector (doneaction:)];Self.navigationItem.rightBarButtonItem = Doneitem;[Doneitem release];26}2728//This method is performed multiple times equivalent to refresh View29-(void) viewwillappear :(BOOL) Animated30 {31 [super viewwillappear: Animated]; 32 tf = (Uitextfield *) [Self.view viewwithtag:1000]; Tf.text = [Appstatus shareinstance].contextstr; 34} 35 36 //pop back to previous page 37-(void) doneaction: (ID) sender38 {39 //single-pass 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
<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"45@implementation Indexviewcontroller67-(void) Dealloc: 99 [[Nsnotificationcenter Defaultcenter] Removeobserver:selfTen name:@"Change_title" Object:nil];11 [Super Dealloc];12}13-(ID) init15 {16if (self = [Super Init])17 {[[Nsnotificationcenter Defaultcenter] Addobserver:selfSelector:@selector (change:)name:@"Change_title"Object:nil];22}23return self;24}2526-(void) Change: (Nsnotification *) Anoti27 {28Notification Pass Value29 nsdictionary *dic = [Anoti userInfo]; 30 nsstring *str = [dic valueforkey:@ 32 uitextfield *TF = (Uitextfield *) [Self.view viewwithtag:1000];33 tf.text = str; 34} 35 36-(void) viewwillappear: (BOOL) Animated37 {38 [super viewwillappear:animated]; /*40//single-case transmission value of Uitextfield *TF = (Uitextfield *) [self.view viewwithtag:1000];42 tf.text = [AppSta Tus shareinstance].contextstr;43 */44}45 46 @end
DetailViewController.h
#import <UIKit/UIKit.h>changedelegate; Notifies the compiler that there is this agent Uiviewcontroller5 {Uitextfield *textfield; 7}@end
Detailviewcontroller.m
1#import "DetailViewController.h"2#import "AppStatus.h"34 @implementation Detailviewcontroller5 @synthesize navititle = _navititle;67-(void) Loadview8 {9 Uibarbuttonitem *doneitem = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemdonetarget: Self action: @selector (doneaction:)];ten self.navigationItem.rightBarButtonItem = Doneitem; [Doneitem release]; 12} //Pop back to previous page -(void) Doneaction: (ID) sendernsdictionary *dic = [nsdictionary DictionaryWithObject:textField.text Forkey:@ "Info"]; [[Nsnotificationcenter Defaultcenter] Postnotificationname:@ "Change_title" Object:nil Userinfo:dic]; [Self.navigationcontroller Popviewcontrolleranimated:yes]; *
IOS various value-transfer methods