• Block 传值


    A 界面:

    - (IBAction)gotoVC:(id)sender {

        //必须在事件发生时调用Block,每次Block对应一次初始化

        cvc = [[CViewController alloc]initWithNibName:@"CViewController" bundle:nil];   

        [cvc returenText:^(NSString *str) {//调用block

            self.A.text = str;

        }];    

        [self presentViewController:cvc animated:YES completion:nil];

    }

    或者:不能再viewDidLoad里面初始化调用block 又使用其他方式跳转(如在viewdidload里面初始化而使用segue跳转,因为segue又初始化一遍了)。

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

        BVCViewController*bvc0 = segue.destinationViewController;

        [bvc0 returenText:^(NSString *str) {

            self.A.text = str;

        }];

    }

    B 界面

    .h 文件

    typedef void(^ReturnTextStr)(NSString*str); //定义一个block

    @property(nonatomic,strong)ReturnTextStr returnStrBlock;//声明一个定义好的block

    -(void)returenText:(ReturnTextStr)block;//block方法

     

    .m文件

    -(void)returenText:(ReturnTextStr)block{

        _returnStrBlock = block;

    }

    - (IBAction)back:(id)sender {   //回调 

        [self dismissViewControllerAnimated:YES completion:^{    

            if (_returnStrBlock != nil ) {

                _returnStrBlock(self.blockTEXT.text);

            }

        }];   

    }

  • 相关阅读:
    各职业岗位说明
    感慨集中所
    批量插入测试数据
    写作技巧
    Cordova学习
    CocoStudio
    maven使用感受
    org.json
    ApplicationContext
    2017
  • 原文地址:https://www.cnblogs.com/mapanguan/p/5455543.html
Copyright © 2020-2023  润新知