Block传值

准备工作,设置跟视图控制器和两个viewController

block的调用

在MainViewController中铺一个button

UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.layer.borderWidth=1;
    button.frame=CGRectMake(100, 100, 150, 30);
    [button setTitle:@"下一页" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

// 通过block来改变self.view的背景颜色
-(void)click:(UIButton *)button
{
    // 没有参数,没有返回值的block
    // 通过block,改变self.view的颜色
    void(^block)()=^(){
        self.view.backgroundColor=[UIColor colorWithRed:(arc4random()%256) /255.0 green:(arc4random()%256) /255.0 blue:(arc4random()%256) /255.0 alpha:0.7];
    };
    // 调用block
    block();
}
从SecondViewController传值到MainViewController上

从后往前进行传值,通过SecondViewController上得button改变MainViewController的背景颜色

// 协议传值

SecondViewController.h

#import <UIKit/UIKit.h>
// 1.声明一份协议
@protocol SecondViewControllerDelegate <NSObject>
// 协议方法
-(void)changeColor;
@end
@interface SecondViewController : UIViewController
// 2.设置代理人属性
@property(nonatomic,retain)id<SecondViewControllerDelegate>delegate;
@end


SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()
@end
@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.layer.borderWidth=1;
    button.frame=CGRectMake(100, 100, 150, 30);
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
// 3.触发方法,  点击返回button时触发
-(void)click:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
    [self.delegate changeColor];
}


MainViewController.m

// 4.引头文件,签订协议
#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()<SecondViewControllerDelegate>
@end
@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.layer.borderWidth=1;
    button.frame=CGRectMake(100, 100, 150, 30);
    [button setTitle:@"下一页" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}

-(void)click:(UIButton *)button
{
    SecondViewController *secVC=[[SecondViewController alloc]init];
    [self.navigationController pushViewController:secVC animated:YES];

    // 5.设置代理人
    secVC.delegate=self;
    [secVC release];

}
// 6.实现方法
-(void)changeColor
{
    self.view.backgroundColor=[UIColor colorWithRed:(arc4random()%256)/255.0 green:(arc4random()%256)/255.0  blue:(arc4random()%256)/255.0  alpha:0.7];
}

block从后往前传值

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
@property(nonatomic,copy)void(^block)(NSString *);
@property(nonatomic,copy)void(^block1)(NSArray *);
@end


MainViewController.m

button的创建相同

// button的点击方法
-(void)click:(UIButton *)button

{
    SecondViewController *secVC=[[SecondViewController alloc]init];


    // 通过block实现从后往前传值

    // block传值不需要返回值,因为调用更需要返回值,所以传值的时候只要参数,不需要返回值

    // 只有参数没有返回值的block
    // 传字符串
    void(^block)(NSString *)=^(NSString *str){
        // 传过来的数据的处理都在block中进行
        NSLog(@"%@",str);
    };
     secVC.block=block;
    // 传数组
    void(^block2)(NSArray *)=^(NSArray *arr){
        NSLog(@"%@",arr);
    };
    secVC.block1=block2;

    [self.navigationController pushViewController:secVC animated:YES];
    [secVC release];

}


SecondViewController.m

button的创建同前面SecondViewController中的

// button的创建方法
-(void)click:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];

     // 调用传过来的block,字符串
    self.block(@"liushanshan");

    // 数组
    self.block1(@[@"shanshan",@"xianhe"]);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值