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:
- @ Protocol ClassBDelegate <NSObject>
- -(Void) methodOne;
- @ Optional
- -(Void) methodTwo :( NSString *) value;
- @ 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:
- @ Interface ClassA <ClassBDelegate>
- @ End
In the implementation file (ClassA. m) of ClassBDelegate, methodTwo can not be implemented, as follows:
- -(Void) methodOne {
- // Specific implementation content
- }
-
- -(Void) methodTwo :( NSString *) value {
- // Specific implementation content
- }
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:
- @ Interface ClassB
- @ Property (nonatomic, unsafe_unretained) id <ClassBDelegate> delegate;
- @ 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.
- [Self. delegate methodOne];
- [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