• IOS传值之Block传值(二)


    @interface QWViewController : UIViewController

    @property(nonatomic,strong)UILabel *label;

    1@property(nonatomic,strong)UITextField *textField;

    @end

    #import "QWViewController.h"

    #import "QWViewControllerTwo.h"

     

    @interface QWViewController ()

    @end

    @implementation QWViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

        label.backgroundColor=[UIColor blueColor];

        self.label=label;

        [self.view addSubview:label];

        

        UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 100, 50)];

        textField.backgroundColor=[UIColor yellowColor];

        self.textField=textField;

        [self.view addSubview:textField];

        

        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];

        button.frame=CGRectMake(100, 300, 100, 50);

        button.backgroundColor=[UIColor blackColor];

        [button setTitle:@"jump" forState:UIControlStateNormal];

        [button addTarget:self action:@selector(doClicked) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:button];

    }

    -(void)doClicked{

        QWViewControllerTwo *ViewControllerTwo=[[QWViewControllerTwo alloc]init];

        ViewControllerTwo.labelText=self.textField.text;//顺传值

        //[ViewControllerTwo.label setText:self.textField.text];//注意:这样传值是不对的。

        //逆向传值回来

        [ViewControllerTwo returnText:^(NSString *showText) {

            self.label.text=showText;

        }];

        [self.navigationController pushViewController:ViewControllerTwo animated:YES];

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    @end

    、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

    #import <UIKit/UIKit.h>

     typedef void(^ReturnTextBlock) (NSString *showText);

     @interface QWViewControllerTwo : UIViewController

    @property(nonatomic,copy)NSString *labelText;

    @property(nonatomic,strong)UILabel *label;

    @property(nonatomic,strong)UITextField *textField;

    @property(nonatomic,copy)ReturnTextBlock returnTextBlock;

    -(void)returnText:(ReturnTextBlock)block;

    @end

    #import "QWViewControllerTwo.h"

     

    @interface QWViewControllerTwo ()

     

    @end

     

    @implementation QWViewControllerTwo

     

    -(UILabel *)label{

        if (_label==nil) {

            _label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

        }

        return _label;

    }

    -(UITextField *)textField{

        if (_textField==nil) {

            _textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 100, 50)];

        }

        return _textField;

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

        label.backgroundColor=[UIColor yellowColor];

        label.text=self.labelText;

        self.label=label;

        [self.view addSubview:self.label];

        

        UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 100, 50)];

        textField.backgroundColor=[UIColor yellowColor];

        self.textField=textField;

        [self.view addSubview:textField];

        //创建返回按钮

        UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];

        [backBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];

        [backBtn setTitle:@"back" forState:UIControlStateNormal];

    }

    -(void)goBack{

        [self.navigationController popViewControllerAnimated:YES];

    }

    //将上一个控制器传过来的block保存在本控制器中,在合适的时候调用

    -(void)returnText:(ReturnTextBlock)block{

        self.returnTextBlock=block;

    }

    //在视图将要消失的时候调用本类的block

    -(void)viewWillDisappear:(BOOL)animated{

        if (self.returnTextBlock !=nil) {

            self.returnTextBlock(self.textField.text);//实现是在之前控制器中

        }

        

    }

    @end

    实现效果:

     

     

  • 相关阅读:
    database backup scripts
    RMAN笔记之备份集和备份片
    数据缓冲区详解
    Oracle数据库中快照的使用
    linux 安装RabbitMQ 注意版本
    转 Oracle12c/11个 Client安装出现"[INS-30131]"错误“请确保当前用户具有访问临时位置所需的权限”解决办法之完整版
    Oracle alert日志中出现:‘Fatal NI connect error 12170’
    Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区
    python基础: String类型
    Python3 urllib模块的使用(转载)
  • 原文地址:https://www.cnblogs.com/Jordandan/p/5046753.html
Copyright © 2020-2023  润新知