准备工作,设置跟视图控制器和两个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"]);
}