zoukankan      html  css  js  c++  java
  • Block传值

    页⾯间的传值,有层级正向和逆向传值。正向传值⼀般⽤属性传值即可。逆向传值有代理传值和我们的block传值。
    //
    //  FirstViewController.m
    //  Block传值
    //
    //  Created by cqy on 16/2/16.
    //  Copyright © 2016年 程清杨. All rights reserved.
    //

    #import "FirstViewController.h"

    @interface FirstViewController (){
        UITextField *field;
    }


    @end

    @implementation FirstViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.title = @"block";
        self.navigationController.navigationBar.barTintColor = [UIColor redColor];
       
        field = [[UITextField alloc] initWithFrame:CGRectMake(20, 80, 300, 40)];
        field.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:field];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"next" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightAction:)];
       

        // Do any additional setup after loading the view.
    }
    - (void)rightAction:(UIBarButtonItem *)sender{
        //响应⽅法中,实现我们再Second视图控制器中声明的block 。这个block再这⾥实现,在Second视图中调⽤,传进来⼀个参数,那个参数就是我们再second视图想要再视图显⽰的数据。这⾥就是str。
       
        SecondViewController *SecondView = [[SecondViewController alloc]init];
        // 给second传值
        SecondView.Str = field.text;
        // 执⾏再second调⽤的block,赋值给first
        SecondView.mb = ^(NSString *str){
            field.text = str;
        };
        [self.navigationController pushViewController:SecondView animated:YES];
      
       
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    /*
    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    @end
     
    //
    //  SecondViewController.h
    //  Block传值
    //
    //  Created by cqy on 16/2/16.
    //  Copyright © 2016年 程清杨. All rights reserved.
    //

    #import <UIKit/UIKit.h>
    // 给⼀个block取名为MyBlock,参数是NSString类型的。
    typedef void (^MyBlock)(NSString *str);
    @interface SecondViewController : UIViewController

    @property(nonatomic,copy) NSString *Str;
    // 定义⼀个block属性
    //在这⾥声明⼀个block变量mb。为的是再其他类中可以⽤到。
    @property(nonatomic,copy)MyBlock mb;
    @end
    //
    //  SecondViewController.m
    //  Block传值
    //
    //  Created by cqy on 16/2/16.
    //  Copyright © 2016年 程清杨. All rights reserved.
    //

    #import "SecondViewController.h"
    @interface SecondViewController (){
        UITextField *field;
    }

    @end

    @implementation SecondViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor lightGrayColor];
       
        field = [[UITextField alloc] initWithFrame:CGRectMake(20, 80, 300, 40)];
        field.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:field];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"on" style:(UIBarButtonItemStyleDone) target:self action:@selector(leftAction:)];
       
       
        // Do any additional setup after loading the view.
    }
    - (void)leftAction:(UIBarButtonItem *)sender{
        // pop之前,调⼀个block传值
        self.mb(field.text);//这⾥调⽤block将field的值作为参数,去到first视图控制器中执⾏。就能实现把值传给上级视图的需求。
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    /*
    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    @end
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    OpenGL学习之路(四)
    OpenGL学习之路(三)
    ajax请求成功后新开窗口window.open()被拦截解决方法
    Mysql日志解析
    Kibana+Logstash+Elasticsearch 日志查询系统
    谈谈Redis的SETNX
    常用前端开发工具合集
    Media Queries 详解
    启动mysql时显示:/tmp/mysql.sock 不存在的解决方法
    找不到mysql.sock,mysql.sock丢失问题解决方法
  • 原文地址:https://www.cnblogs.com/iQingYang/p/5193232.html
Copyright © 2011-2022 走看看