Several modes of transmission between IOS pages (attribute, proxy, block, single, Notification)
Last Update:2015-05-03
Source: Internet
Author: User
<span id="Label3"></p><p class="postTitle"><p class="postTitle"><span style="font-size: 18px;">Lable in the second interface displays the text in the first interface TextField</span></p></p><p><p><span style="font-size: 18px;">First we set up a rootviewcontrollers and a detailviewcontrollers, declare a TextString property in detailviewcontrollers, to receive the passed String.</span></p></p><p><p></p></p><p><p></p></p><p><p><span style="font-size: 18px;">Also create a lable to display the passed string</span></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p><span style="font-size: 18px;">Introduce detailviewcontrollers on Rootviewcontrollers and declare a TextField property to enter the string</span></p></p><p><p></p></p><p><p></p></p><p><p><span style="font-size: 18px;">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</span></p></p><p><p></p></p><p><p></p></p><p><p></p></p>(ii) Block Transfer value<p class="postTitle"><p class="postTitle"><span style="font-size: 18px;">The value of the block is also passed from the second interface to the first interface.</span></p></p><p class="postTitle"><p class="postTitle"><span style="font-size: 18px;">First we are in the Detailviewcontrollers. H file, the properties</span></p></p><p><p></p></p><p><p></p></p><p><p><span style="font-size: 18px;">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</span></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p>(iii) Proxy Value<p><p><span style="font-size: 18px;">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</span></p></p><p><p><span style="font-size: 18px;">First we create the Protocol method in the DetailViewControllers.h file</span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p></p></p><p><p><span style="font-size: 18px;">In Detailviewcontrollers. m when we determine that the proxy object exists, bind the method to it</span></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p></p></p><p><p><span style="font-size: 18px;">Rootviewcontrollers. m Files We specify the agent and let it execute the proxy method</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;"></span></p></p><p><p></p></p><p><p></p></p>(iv) single-case Pass-through value<p><p></p></p><p><p><strong style="font-size: 16px; line-height: 1.5;">Single-pass value (for Sharing)</strong></p></p><p><p><span style="font-size: 16px;">APPSTATUS.H Create a singleton class Appstatus</span></p></p><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><pre><pre><span style="font-size: 16px;">1 #import <Foundation/Foundation.h> 2 3 @interface appstatus:nsobject 4 {5 nsstring *_contextstr; 6} 7 <c 9 />8 @property (nonatomic,retain) nsstring *contextstr; 9 + (appstatus *) shareinstance;11 @end</span></pre></pre><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><p><p><span style="font-size: 16px;">Appstatus.m</span></p></p><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><pre><pre><span style="font-size: 16px;">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< c10/>}15 return _instance;16}17-(id) init19 {(self = [super init]) {$ }24 return self;25}26-(void) dealloc28 { [super dealloc];30}31 @end</span></pre></pre><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><p><p><span style="font-size: 18px;">RootViewController.h</span></p></p><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><pre><span style="font-size: 16px;"> 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 </span></pre><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><p><p><span style="font-size: 18px;">DetailViewController.h</span></p></p><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><pre><pre><span style="font-size: 16px;">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</span></pre></pre><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><p><p><span style="font-size: 18px;">Detailviewcontroller.m</span></p></p><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><pre><span style="font-size: 16px;"> 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}</span></pre><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span>(V) notification of the value of the transfer<p><p><span style="font-size: 18px;">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</span></p></p><p><p><span style="font-size: 18px;">A page RootViewController.h</span></p></p><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><pre><pre><span style="font-size: 16px;">1 #import <uikit/uikit.h>2 #import "DetailViewController.h" 3 @interface Rootviewcontroller:uiviewcontroller <changedelegate>4 {5 </span></pre></pre><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><p><p><span style="font-size: 16px;">A page rootviewcontroller.m</span></p></p><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><pre><span style="font-size: 16px;"> 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</span></pre><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><p><p><span style="font-size: 16px;">DetailViewController.h</span></p></p><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><pre><pre><span style="font-size: 16px;">1 #import <uikit/uikit.h>2 @protocol changedelegate;//notification compiler has this agent 3 4 @interface detailviewcontroller: UIViewController5 {6 Uitextfield *textfield;7}8 @end</span></pre></pre><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><p><p><span style="font-size: 16px;">Detailviewcontroller.m</span></p></p><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><pre><pre><span style="font-size: 16px;">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}</span></pre></pre><span class="cnblogs_code_copy" style="font-size: 16px;"><span class="cnblogs_code_copy" style="font-size: 16px;"></span></span><p><p></p></p><p><p>Several modes of transmission between IOS pages (attribute, proxy, block, single, Notification)</p></p></span>