Several ways of transferring values between IOS pages (attributes, proxies, blocks, singleton, notifications) (RPM)

Source: Internet
Author: User
Tags notification center uikit

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-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];     }15     return _instance;16}17-(ID) init19 {(Self     = [Super init])     {     }24< C20/>return self;25}26-(void) dealloc28 {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

1. Registration Notice

2, Notification Center send a message notification, where name must be the same before and after

3, implement the method inside the notification center, and realize the value

4, the message sent out, to remove. (When the page is going to disappear)

A page rootviewcontroller.m

Secondviewcontroller.m

Several ways of transferring values between IOS pages (attributes, proxies, blocks, singleton, notifications) (RPM)

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.