block 回调传值

来源:互联网 发布:sql语句统计女人数数量 编辑:程序博客网 时间:2024/06/16 11:55

干货:

      咱们分为1页2页,情况是1页向2页跳,2页往1页传值 1页得到传回的值

       首先在2页的.h ,定义block

#import <UIKit/UIKit.h>


typedef void(^changeType)(NSString *);


@interface firstViewController :UIViewController


@property(nonatomic,copy)changeType callBackBlock;


@end

     
     在传值的页面的.m 使用并把值带回
     在往回跳的方法中写:

   

  - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

      NSString *str =@"我是一个小红帽";

     self.callBackBlock(str);

     [selfdismissViewControllerAnimated:YEScompletion:nil];

  }


     在1页往2页跳的方法中可以得到2页传回的值

  -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

      firstViewController *firVC = [[firstViewControlleralloc] init];

      firVC.callBackBlock = ^(NSString *str){ 

        NSLog(@"%@",str);

      };

      [selfpresentViewController:firVCanimated:YEScompletion:nil];

   }


    这样就得到需要的值了.

0 0