Several modes of transmission between IOS pages (attribute, proxy, block, single, Notification)

Source: Internet
Author: User
Tags notification center uikit
<span id="Label3"></p><p class="postTitle"><p class="postTitle">Lable in the second interface displays the text in the first interface TextField</p></p><p><p>First we set up a rootviewcontrollers and a detailviewcontrollers, declare a TextString property in detailviewcontrollers, to receive the passed String.</p></p><p><p></p></p><p><p></p></p><p><p>Also create a lable to display the passed string</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Introduce detailviewcontrollers on Rootviewcontrollers and declare a TextField property to enter the string</p></p><p><p></p></p><p><p></p></p><p><p>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</p></p><p><p></p></p><p><p></p></p><p><p></p></p>(ii) Block Transfer value<p class="postTitle"><p class="postTitle">The value of the block is also passed from the second interface to the first interface.</p></p><p class="postTitle"><p class="postTitle">First we are in the Detailviewcontrollers. H file, the properties</p></p><p><p></p></p><p><p></p></p><p><p>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</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p>(iii) Proxy Value<p><p>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</p></p><p><p>First we create the Protocol method in the DetailViewControllers.h file</p></p><p><p></p></p><p><p></p></p><p><p>In Detailviewcontrollers. m when we determine that the proxy object exists, bind the method to it</p></p><p><p></p></p><p><p></p></p><p><p>Rootviewcontrollers. m Files We specify the agent and let it execute the proxy method</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p>(iv) single-case Pass-through value<p><p></p></p><p><p><strong>Single-pass value (for Sharing)</strong></p></p><p><p>APPSTATUS.H Create a singleton class Appstatus</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1 #import <Foundation/Foundation.h> 2 3 @interface appstatus:nsobject 4 {5 nsstring *_contextstr; 6} 7 <c 2 />8 @property (nonatomic,retain) nsstring *contextstr; 9 + (appstatus *) shareinstance;11 @end</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Appstatus.m</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>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]; }15 return _instance;16}17-(id) init19 {(self = [super init]) { }24< C20/>return self;25}26-(void) dealloc28 {dealloc];30-}31- @end</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>RootViewController.h</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 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 </pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>DetailViewController.h</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>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</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Detailviewcontroller.m</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 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}</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>(V) notification of the value of the transfer<p><p>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</p></p><p><p>1. Registration Notice</p></p><p><p>2, Notification Center send a message notification, where name must be the same before and after</p></p><p><p>3, implement the method inside the notification center, and realize the value</p></p><p><p>4, the message sent out, to Remove. (when The page is going to Disappear)<br><br></p></p><p><p>A page rootviewcontroller.m</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Secondviewcontroller.m</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Several modes of transmission between IOS pages (attribute, proxy, block, single, Notification)</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.