Several value passing methods (attribute, proxy, block, Singleton, notification) and iosblock between iOS pages

Source: Internet
Author: User

Several value passing methods (attribute, proxy, block, Singleton, notification) and iosblock between iOS pages
How does the second view controller obtain partial information of the first View Controller? For example, lable In the second interface displays the text in textField of the first interface.

This requires attribute value passing and block value passing.

Then how does the first View Controller obtain part of the second view controller?

For example, lable In the first interface displays the text in textField In the second interface.

In this case, you need to use a proxy to pass the value.

There are eight methods for transferring values between pages. Next we will briefly introduce the five common methods for transferring values between pages:

(1) attribute value passing 

Lable In the second interface displays the text in textField In the first Interface

First, create a RootViewControllers and a DetailViewControllers. Declare a textString attribute in DetailViewControllers to receive the passed string,

 

Create a Lable to display the passed strings.

 

 

Introduce DetailViewControllers on RootViewControllers and declare a textField attribute to input a string

 

Then, on RootViewControllers, we create and add a button. When a button is clicked, the corresponding method is returned to switch between views to complete value transfer between views.

 

 

(2) Block value passing

The block value is also transferred from the second interface to the first interface.

First, in the. h file of DetailViewcontrollers

 

In the. m file of RootViewControllers, the others remain unchanged. In the response method of the button, we assign a value to the block attribute to complete the block passing value.

 

 

 

(3) pass values by proxy

The RootViewControllers page is pushed to the DetailViewControllers page. If you want to return the information on the DetailViewControllers page (callback) to the RootViewControllers page, use the proxy to pass the value. The DetailViewControllers defines the protocol and declaration proxy, rootViewControllers acts as the proxy for DetailViewControllers

First, create the Protocol method in the DetailViewControllers. h file.

 

In the. m of DetailViewControllers, when we determine that the proxy object exists, bind the corresponding method to it.

 

In the. m file of RootViewControllers, we specify the proxy and let it execute the proxy method.

 

 

 

(4) Single-case data transfer

 

Single case transfer value (SHARE)

AppStatus. h create a singleton AppStatus

 1 #import <Foundation/Foundation.h> 2  3 @interface AppStatus : NSObject 4 { 5     NSString *_contextStr; 6 } 7  8 @property(nonatomic,retain)NSString *contextStr; 9 10 +(AppStatus *)shareInstance;11 12 @end

AppStatus. m

 1 #import "AppStatus.h" 2  3 @implementation AppStatus 4  5 @synthesize contextStr = _contextStr; 6  7 static AppStatus *_instance = nil; 8  9 +(AppStatus *)shareInstance10 {11     if (_instance == nil)12     {13         _instance = [[super alloc]init];14     }15     return _instance;16 }17 18 -(id)init19 {20     if (self = [super init])21     {22         23     }24     return self;25 }26 27 -(void)dealloc28 {29     [super dealloc];30 }31 32 @end

RootViewController. h

1 # import "RootViewController. h "2 # import" DetailViewController. h "3 # import" AppStatus. h "4 5 @ interface RootViewController () 6 7 @ end 8 9 @ implementation RootViewController10 11-(void) loadView12 {13 // core code 14 UIButton * btn = [UIButton buttonWithType: UIButtonTypeRoundedRect]; 15 btn. frame = CGRectMake (0, 0,100, 30); 16 [btn setTitle: @ "Push" forState: 0]; 17 [btn addTarget: self action: @ selector (pushAction :) forControlEvents: UIControlEventTouchUpInside]; 18 [self. view addSubview: btn]; 19} 20 21-(void) pushAction :( id) sender22 {23 tf = (UITextField *) [self. view viewWithTag: 1000]; 24 25 // Save the information to be passed in a single case (Sharing) 26 // [[AppStatus transfer instance] setContextStr: tf. text]; this is equivalent to the following method: 27 [AppStatus verification instance]. contextStr = tf. text; 28 // navigation push to the next page 29 // pushViewController incoming stack reference count + 1, and control to system 30 DetailViewController * detailViewController = [[DetailViewController alloc] init]; 31 32 // navigate to push to the next page 33 [self. navigationController pushViewController: detailViewController animated: YES]; 34 [detailViewController release]; 35} 36 37 @ end

DetailViewController. h

1 # import <UIKit/UIKit. h> 2 @ protocol ChangeDelegate; // notify the compiler of this proxy 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 @ implementation DetailViewController 9 10 @ synthesize naviTitle = _ naviTitle; 11 12-(void) loadView13 {14 self. view = [[[UIView alloc] initWithFrame: CGRectMake (0, 0,320,480)] autorelease]; 15 16 // example 17 self. title = [AppStatus attached instance]. contextStr; 18 textField = [[UITextField alloc] initWithFrame: CGRectMake (100,100,150, 30)]; 19 textField. borderStyle = UITextBorderStyleLine; 20 [self. view addSubview: textField]; 21 [textField release]; 22 23 UIBarButtonItem * doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: identifier: self action: @ selector (doneAction. navigationItem. rightBarButtonItem = doneItem; 25 [doneItem release]; 26} 27 28 // This method is equivalent to refreshing view29-(void) viewWillAppear :( BOOL) animated30 {31 [super viewWillAppear: animated]; 32 tf = (UITextField *) [self. view viewWithTag: 1000]; 33 tf. text = [AppStatus attached instance]. contextStr; 34} 35 36 // pop back to the previous page 37-(void) doneAction :( id) sender38 {39 // the value of a single example is 40 [AppStatus reset instance]. contextStr = textField. text; 41 [self. navigationController popToRootViewControllerAnimated: YES]; 42}
(5) Notification transfer value

When the value of a notification changes, the subscriber must register the notification.

Page A RootViewController. h

1 #import <UIKit/UIKit.h>2 #import "DetailViewController.h"3 @interface RootViewController : UIViewController<ChangeDelegate>4 {5     UITextField *tf;6 }7 @end 

Page A RootViewController. m

1 # import "IndexViewController. h "2 # import" DetailViewController. h "3 # import" AppStatus. h "4 5 @ implementation IndexViewController 6 7-(void) dealloc 8 {9 [[nsicationicationcenter defaultCenter] removeObserver: self10 name: @" CHANGE_TITLE "object: nil]; 11 [super dealloc]; 12} 13 14-(id) init15 {16 if (self = [super init]) 17 {18 [[nsicationicationcenter defacenter center] addObserver: self19 selector: @ selector (change :) 20 name: @ "CHANGE_TITLE" 21 object: nil]; 22} 23 return self; 24} 25 26-(void) change :( NSNotification *) aNoti27 {28 // notification value 29 NSDictionary * dic = [aNoti userInfo]; 30 NSString * str = [dic valueForKey: @ "Info"]; 31 32 UITextField * tf = (UITextField *) [self. view viewWithTag: 1000]; 33 tf. text = str; 34} 35 36-(void) viewWillAppear :( BOOL) animated37 {38 [super viewWillAppear: animated]; 39/* 40 // The value of 41 UITextField * tf = (UITextField *) [self. view viewWithTag: 1000]; 42 tf. text = [AppStatus attached instance]. contextStr; 43 */44} 45 46 @ end

DetailViewController. h

1 # import <UIKit/UIKit. h> 2 @ protocol ChangeDelegate; // notify the compiler of this proxy 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 naviTitle = _ naviTitle; 6 7-(void) loadView 8 {9 UIBarButtonItem * doneItem = [[UIBarButtonItem alloc] failed: Failed: self action: @ selector (doneAction :)]; 10 self. navigationItem. rightBarButtonItem = doneItem; 11 [doneItem release]; 12} 13 14 // pop back to the previous page 15-(void) doneAction :( id) sender16 {17 NSDictionary * dic = [NSDictionary dictionaryWithObject: textField. text forKey: @ "Info"]; 18 19 [[nsnotifcenter center defacenter center] postNotificationName: @ "CHANGE_TITLE" object: nil userInfo: dic]; 20 21 [self. navigationController popViewControllerAnimated: YES]; 22 23}

 

Related Article

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.