iOS 多界面传值之--Block(代码块)传值

Block传值

一般应用于逆向传值,即第二界面向第一界面传值,我们需要记住的两点:
1.要在第二个界面(SecondViewController.h)定义一个Block:
2.在第一个界面(ViewController.m)跳转第二个界面的方法中我们为block属性赋值完成block传值:

3.首先来到SecondViewController.h定义一个代码块

#import <UIKit/UIKit.h>

//定义Block
typedef void (^PushBlock)(NSString *);

//声明Block属性
@interface SecondViewController : UIViewController

@property (nonatomic,strong) PushBlock pushValueString;

@end

4.在SecondViewController,m里面设置UITextField 和 UIbutton 成为属性,然后在viewDidLoad 方法里面分别创建一个textField 和 一个button


#import "SecondViewController.h"

@interface SecondViewController ()


@property (nonatomic,strong) UITextField *textF;
@property (nonatomic,strong) NSString *textString;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];
    _textF = [[UITextField alloc]initWithFrame:CGRectMake(0, 250, self.view.bounds.size.width, 50)];
    _textF.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textF];


    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(20, 350, self.view.bounds.size.width-40, 50);
    btn.backgroundColor = [UIColor purpleColor];
    btn.titleLabel.font = [UIFont systemFontOfSize:25.0];
    btn.tintColor = [UIColor whiteColor];
    [btn setTitle:@"Back" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}

5.实现button里面的点击事件的方法,并且在这方法里面实现Block的核心代码

-(void)backAction:(UIButton *)sender{
//    核心代码
    _pushValueString (_textF.text);
    [self dismissViewControllerAnimated:YES completion:nil];


}

6.回到ViewController里面,首先我们需要导入一下头文件#import “SecondViewController.h”,并且创建一个UITextField 和 UIbutton

#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()

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

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    /**
     *  block传值(场景)一般用于逆向传值,即第二个界面传值给第一个界面
        1.要在第二个界面(SecondViewController.h)定义一个Block:
        2.要在第一个界面(ViewController.m)跳转第二个界面的方法中我们为block属性赋值完成block传值:
     */


    self.view.backgroundColor = [UIColor greenColor];
    self.title = @"Block传值";
     _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 250, self.view.bounds.size.width, 50)];
    _textField.borderStyle = UITextBorderStyleRoundedRect;

    [self.view addSubview:_textField];


    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(20, 350, self.view.bounds.size.width-40, 50);
    btn.titleLabel.font = [UIFont systemFontOfSize:25.0];
    btn.backgroundColor = [UIColor blueColor];
    btn.tintColor = [UIColor whiteColor];
    [btn setTitle:@"Next" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];


}

7.实现一下button的点击事件的方法,并在这个方法里面实现跳转界面和block的核心代码属性赋值

-(void)nextAction:(UIButton *)sender{

    SecondViewController *secondVC = [[SecondViewController alloc]init];
//    核心代码为block属性赋值
    secondVC.pushValueString = ^(NSString *string){
        _textField.text = string;

    };
    [self presentViewController:secondVC animated:YES completion:nil];



}

8.由于我们用的是模态视图进行页面之间的跳转,所以就不需要去创建UINavigationController了.模拟器运行效果如下
这里写图片描述

这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值