//联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄
/**
注意点: 1.看 GIF 效果图.
2.看连线视图的效果图.
3.看实现代码(直接复制实现效果).
4.下载demo链接:(点击或者是https://gitee.com/ShiHu132/BlockChuanZhi)
*/
一、GIF 效果图:
二、连线视图的效果图:
图1:
图2:
三、实现代码:
=========================
===================================================
==========================
控制器1:ViewController.m
// ViewController.m
// Block传值~demo
//
// Created by石虎on 2017/9/22.
// Copyright © 2017年石虎. All rights reserved.
//
#import"ViewController.h"
#import"SHBlockViewController.h"
@interfaceViewController()
@property(weak,nonatomic)IBOutletUILabel*blockLabel;
- (IBAction)blockClick:(UIButton*)sender;
@end
@implementationViewController
//逆向传值
- (IBAction)blockClick:(UIButton*)sender {
SHBlockViewController*blockVC =[[SHBlockViewControlleralloc]init];
//接收方只需要调用传输方的block方法
[blockVCshowTheResultToFirst:^(NSString*secondString) {
self.blockLabel.text= secondString;
}];
[selfpresentViewController:blockVCanimated:YEScompletion:nil];
}
- (void)viewDidLoad {
[superviewDidLoad];
}
@end
================
=======
控制器2:SHBlockViewController.m
//
// SHBlockViewController.m
// Block传值~demo
//
// Created by石虎on 2017/9/22.
// Copyright © 2017年石虎. All rights reserved.
//
#import"SHBlockViewController.h"
#import"ViewController.h"
@interfaceSHBlockViewController()
@property(weak,nonatomic)IBOutletUITextField*startValueField;
- (IBAction)startValueClick:(UIButton*)sender;
@end
@implementationSHBlockViewController
//返回到上级界面
- (IBAction)startValueClick:(UIButton*)sender {
[selfdismissViewControllerAnimated:YEScompletion:nil];
}
// block实际执行的内容
-(void)showTheResultToFirst:(SHBlockViewBlock)block {
self.passValue= block;
}
//在即将消失view时,将block方法的参数设置为文本框的内容
-(void)viewWillDisappear:(BOOL)animated {
if(self.passValue!=nil) {
self.passValue(self.startValueField.text);
}
}
//退出键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event {
[self.viewendEditing:YES];
}
================
=======