Value Transfer Agent for iOS and value transfer agent for ios

Source: Internet
Author: User

Value Transfer Agent for iOS and value transfer agent for ios

There are several methods for transmitting values in iOS, including: pass value by proxy, pass value by block, pass value by attribute, transfer value by notification, pass value by single case, and pass value by userdefault or file, generally, value passing by proxy and block are the most common methods. This article describes how to pass value by proxy. Other methods will be written in the blog later.

1. What is a delegated proxy?

1. protocol (protocol): After using this protocol, you must handle things according to the content specified in the protocol. The methods required in the protocol must be implemented (except for the @ optional method ).

Protocol is a syntax that provides a very convenient opportunity to implement the delegate mode.

Define protocol as follows:

  1. @ Protocol ClassBDelegate <NSObject>
  2. -(Void) methodOne;
  3. @ Optional
  4. -(Void) methodTwo :( NSString *) value;
  5. @ End

Defines a ClassB protocol, which contains two methods, where methodTwo is optional.

The following code implements this protocol in the header file (ClassA. h) of ClassA:

  1. @ Interface ClassA <ClassBDelegate>
  2. @ End

In the implementation file (ClassA. m) of ClassBDelegate, methodTwo can not be implemented, as follows:

  1. -(Void) methodOne {
  2. // Specific implementation content
  3. }
  4.  
  5. -(Void) methodTwo :( NSString *) value {
  6. // Specific implementation content
  7. }

2. As the name suggests, a proxy is to entrust others to handle affairs. When one thing happens, you will not handle it yourself and let others handle it.

Delegate and protocol are irrelevant. Delegate is a design pattern. Is to let another class (or itself) complete part of the thing that one class needs to do.

Define a proxy in the header file (ClassB. h) of ClassB as follows:

  1. @ Interface ClassB
  2. @ Property (nonatomic, unsafe_unretained) id <ClassBDelegate> delegate;
  3. @ End

In this way, when we encounter problems that we want other classes (such as ClassA) to handle in the implementation file (ClassB. m) of ClassB, we can do this.

  1. [Self. delegate methodOne];
  2. [Self. delegate methodTwo: @ "value to be passed"];

II. Specific instance

The function is to create a UIButton button in viewcontroller for pushing to the next page, and a UILable for displaying the text returned from the second page, in secondCreate a UITextfield in viewcontroller for text input. After the input is complete, press back to return to the first page, the entered text is displayed on the lable.

1 Project

 

2 ViewController. h file

# Import <UIKit/UIKit. h>

@ Interface ViewController: UIViewController

@ End

3 ViewController. m file

# Import "ViewController. h"

# Import "SecondViewController. h"

 

@ Interface ViewController () <getTextFromDelegate>

{

UIButton * _ button;

UILabel * _ lable;

}

@ End

@ Implementation ViewController

-(Void) viewDidLoad {

[Super viewDidLoad];

[Self initLayout];

}

-(Void) initLayout {

_ Button = [UIButton buttonWithType: UIButtonTypeSystem];

_ Button. frame = CGRectMake (0, 0,100, 50 );

_ Button. center = self. view. center;

_ Button. backgroundColor = [UIColor redColor];

[_ Button addTarget: self action: @ selector (pushToNextViewController :) forControlEvents: UIControlEventTouchUpInside];

[_ Button setTitle: @ "next page" forState: UIControlStateNormal];

[Self. view addSubview: _ button];

_ Lable = [[UILabel alloc] initWithFrame: CGRectMake (10, 64,355,200)];

_ Lable. backgroundColor = [UIColor orangeColor];

[Self. view addSubview: _ lable];

}

-(Void) pushToNextViewController :( UIButton *) sender {

SecondViewController * secondVC = [SecondViewController new];

// Proxy is itself

SecondVC. delegate = self;

[Self. navigationController pushViewController: secondVC animated: YES];

}

# Proxy method implemented by pragma mark

-(Void) getText :( NSString *) text {

_ Lable. text = text;

}

-(Void) didReceiveMemoryWarning {

[Super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@ End

4 SecondViewController. h file

# Import <UIKit/UIKit. h>

@ Protocol getTextFromDelegate <NSObject>

// Declare the Protocol Method

-(Void) getText :( NSString *) text;

@ End

@ Interface SecondViewController: UIViewController

@ Property (weak, nonatomic) id <getTextFromDelegate> delegate;

@ End

4 SecondViewController. m file

# Import "SecondViewController. h"

@ Interface SecondViewController () <getTextFromDelegate>

{

UITextField * _ textField;

}

@ End

@ Implementation SecondViewController

-(Void) viewDidLoad {

[Super viewDidLoad];

Self. view. backgroundColor = [UIColor whiteColor];

_ TextField = [[UITextField alloc] initWithFrame: CGRectMake (0, 0,200, 50)];

_ TextField. backgroundColor = [UIColor redColor];

_ TextField. center = self. view. center;

[Self. view addSubview: _ textField];

}

# When the pragma mark is about to disappear

-(Void) viewWillDisappear :( BOOL) animated {

// Pass the value obtained on this page to the previous page for implementation

[Self. delegate getText: _ textField. text];

}

-(Void) didReceiveMemoryWarning {

[Super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@ End

Result of three Simulators

 

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.