六种传值方式之Block传值

使用Block在A、B页面实现传值
Block使用起来很方便,不过这里没有将其作为方法的一个参数,而是简单的作为一个属性来使用。

思路如下:

在B页面
1.定义一个Block别名

//别名(申明) 类型 ----> void (^) (NSString *text)typedef       
void(^ChangeTextBlock) (NSString *text);

2.定义一个block

  @property (nonatomic, copy) ChangeTextBlock block;

3.在B页面的Button事件响应方法里面修改值

//修改值 -----> 调用block来修改值
if (self.block != nil) { 
  self.block(textField.text);
 }

4.将值传到A页面

  //1. 实现block,使用__block的原因:是防止内存的泄露__block     
  RootViewController *rootVC = self;
  //2. block的初始化
  _modalViewController.block = ^(NSString *text) { 
      //如果不用__block的对象,那么这里会提示一个关于内存的警告      
      UILabel *label = (UILabel *) [rootVC.view viewWithTag:1000];  
      label.text = text;
  };

完整代码:
A页面

.h文件

#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end

.m文件

  #import "RootViewController.h"
  #import "ModalViewController.h"

  @interface RootViewController () { 
        ModalViewController *_modalViewController;
    }
  @end
  
  @implementation RootViewController
  
   - (void)viewDidLoad { 
          [super viewDidLoad];
      
          //1) 设置背景颜色 
          self.view.backgroundColor = [UIColor cyanColor]; 
          //2) 设置一个Label 
          //a) 创建一个Label 
          UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 100, 270, 30)]; 
          //b) 设置该label的tag label.tag = 1000; 
          //c) 设置label的内容 
          label.text = @"Block的传值"; 
           //d) 设置背景颜色 
          label.backgroundColor = [UIColor orangeColor]; 
          //e) 设置字体颜色 
          label.textColor = [UIColor whiteColor]; 
          //f) 设置居中方式 
          label.textAlignment = NSTextAlignmentCenter; 
          //g) 添加label 
          [self.view addSubview:label]; 
          //3) 设置跳转你的button 
          //a) 创建button 
          UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; 
          //b) 设置其frame 
          button.frame = CGRectMake(0, 0, 200, 30); 
          //c) 设置其在屏幕的中心 
          button.center = self.view.center; 
          //d) 设置背景颜色               
          button.backgroundColor = [UIColor lightGrayColor]; 
          //e) 设置显示的内容 
          [button setTitle:@"跳转" forState:UIControlStateNormal]; 
          //f) 设置相应事件 
          [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
          //g) 添加到页面上 
          [self.view addSubview:button]; 
          //4) 初始化模态视图 
          _modalViewController = [[ModalViewController alloc] init]; 
          }

          #pragma mark - 点击的按键响应
          - (void) buttonAction: (UIButton *) button { 
          //1. 实现block,使用__block的原因:是防止内存的泄露 __block  
         RootViewController *rootVC = self; 
          //2. block的初始化 
          _modalViewController.block = ^(NSString *text) { 
          //如果不用__block的对象,那么这里会提示一个警告 ------> 内存的 
          UILabel *label = (UILabel *) [rootVC.view viewWithTag:1000];   
        label.text = text;
           };
          //3. 弹窗模式
           _modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
              //4. 模态视图 
              [self presentViewController:_modalViewController animated:YES completion:nil]; 
          }
          
          @end

B页面

.h文件

#import <UIKit/UIKit.h>

//这里要定义一个block的别名(申明) 类型 ----> void (^) (NSString *text)
typedef void(^ChangeTextBlock) (NSString *text);

@interface ModalViewController : UIViewController

//定义一个block
@property (nonatomic, copy) ChangeTextBlock block;

@end

.m文件

#import "ModalViewController.h"

@interface ModalViewController ()
@end

@implementation ModalViewController

- (void)viewDidLoad { 
        [super viewDidLoad]; 

        //1) 设置背景颜色 
        self.view.backgroundColor = [UIColor orangeColor]; 
        //2) 设置一个TextField 
        //a) 创建一个TextField 
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(25, 100, 270, 30)]; 
        //b) 设置tag 
        textField.tag = 2000; 
        //c) 设置显示的效果 
        textField.borderStyle = UITextBorderStyleRoundedRect; 
        //d) 显示提示语 
        textField.placeholder = @"请输入一段文字...";
         //e) 添加到self.view上
         [self.view addSubview:textField]; 
        //3) 设置跳转你的button 
        //a) 创建button 
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; 
        //b) 设置其frame 
        button.frame = CGRectMake(0, 0, 200, 30);
         //c) 设置其在屏幕的中心 
        button.center = self.view.center; 
        //d) 设置背景颜色 
        button.backgroundColor = [UIColor lightGrayColor]; 
        //e) 设置显示的内容 
        [button setTitle:@"返 回" forState:UIControlStateNormal]; 
        //f) 设置相应事件 
        [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside]; 
        //g) 添加到页面上 
        [self.view addSubview:button]; 
        }
        
   - (void) backAction: (UIButton *) button { 
        //1) 取值 
        UITextField *textField = (UITextField *)[self.view viewWithTag:2000]; 
        //2) 修改值 -----》 调用block来修改值 
        if (self.block != nil) { 
              self.block(textField.text); 
          } 
        //3) 关闭模态视图 
        [self dismissViewControllerAnimated:YES completion:nil]; 
      }

     @end

源代码地址:
https://github.com/zoushuyue/BlockByValue

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,589评论 9 467
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,813评论 1 92
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,217评论 30 472
  • 也许很多人都说自己从未认真地年轻,也总说自己羡慕那些他们觉得认真地年轻的人,可是只要从心地做事,我们就是认真地年轻...
    陈晓森啊阅读 159评论 0 1