I. The block method passes a value from a child view to the parent view. Case: Use the block Mode child view to pass values to the parent view. In the child view. h defines the block, defining the method. The. m file instantiation method. Then the primary view instantiates the child views by using block and passing in the corresponding code, executing the incoming fast code of the parent view under the Addtarget clicked by the child view. Block this process is clear, less than the jump of the proxy protocol.
- . Define FAST Code
-
- In the. h file of the child view
//定义快代码
typedef void(^textFieldChangedBlock)(NSString *text);
- (id)initWithBlock:(textFieldChangedBlock)block;
- Defining instantiation methods
-
//实例化方法
-(id)initWithBlock:(textFieldChangedBlock)block
{
self = [super init];
if (self) {
_changedBlock = block;
}
return self;
}
//定义成员巴拉ing接收块代码
{ textFieldChangedBlock _changedBlock;}
- Incoming fast code when instantiated
-
- Use fast code when the parent view instantiates a child view, and write the corresponding code inside the block code.
DetailViewController *detail = [[DetailViewController alloc]initWithBlock:^(NSString *text) {
[self.label setText:text];
}];
//Presentviewcontroller is the rollout is a new view that is the child view detail
[self presentViewController:detail animated:YES completion:nil];
- Need to execute block code
-
- Executes the fast code passed in by the parent view when needed:_changedblock(self. TextField . text );
- (void)click
{
//返回
_changedBlock(self.textField.text);
[self dismissViewControllerAnimated:YES completion:nil];
}
two. The proxy protocol method passes values from a child view to the parent view.
-
The child view defines the protocol @protocal
@protocol DetailViewControllerDelegate <NSObject>
- (void)detailDone:(NSString *)text;
@end
- define proxy @property (weak,nonatomic) id<myheaddelegate>delegate;
@property ( weak nonatomic ID < > delegate
-
Agent execution Protocol method
- (void)detailDone:(NSString *)text
{
[self.label setText:text];
}
-
@interface Span class= "Typ" >mainviewcontroller : < span> uiviewcontroller < >
@end
-
Parent View Execution Agent method
- (void)click
{
//返回
[_delegate detailDone:self.textField.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
-
Set proxy [Header setdeleagte:self]
- (void)click
{
DetailViewController *detail = [[DetailViewController alloc]init];
[detail setDelegate:self];
[self presentViewController:detail animated:YES completion:nil];
}
From for notes (Wiz)
Comparison of IOS agent and block