zoukankan      html  css  js  c++  java
  • 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

    实现效果:

     

     

  • 相关阅读:
    eclipse 开始运行提示 Java was started but returned exit code=13
    c# silverlight
    CSS 文本、字体、链接
    IIS8中添加WCF支持几种方法小结[图文]
    CSS 背景
    如何创建 CSS
    CSS 简介、语法、派生选择器、id 选择器、类选择器、属性选择器
    HTML 5 服务器发送事件、Input 类型、表单元素、表单属性
    HTML 5 Web 存储、应用程序缓存、Web Workers
    asp.net运行时(二)httpHandle
  • 原文地址:https://www.cnblogs.com/Jordandan/p/5046753.html
Copyright © 2011-2022 走看看