block 代码块 也称作闭包 与C语言函数类似 具有反向传值、回调的功能
block公式分两种:
①声明和实现写到一块的公式
返回值类型(^block名字)(参数列表 参数类型 参数名) = ^(参数列表 参数类型 参数名){
实现代码(如果有返回值 需要return 返回值类型的值)
};
调用:block名字(参数);
②声明和实现分开写的公式
(1)声明
返回值类型(^block名字)(参数列表);
(2)实现
block名字 = ^(参数列表){
实现代码(如果有返回值 需要return 返回值类型的值)
};
(3)调用
block名字(实参);
注? 在调用之前必须有实现的方法
今天在这里主要将一下block的方向传值(方向传值就是在回调的时候给他一个值),通过button的示例来讲一下block怎么反向传值。
具体代码如下:
在AppDelegate.m中创建带导航栏的窗口
#import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]]; [self.window makeKeyAndVisible]; return YES; } @end
在ViewController.m文件里面创建两个button,并且创建一个继承于UIViewController名叫BlockViewController类,导入ViewController.m文件里面
#import "ViewController.h" #import "BlockViewController.h" @interface ViewController () { UIButton *testButton; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self createButton]; } - (void)createButton{ //放图片的button testButton = [UIButton buttonWithType:UIButtonTypeCustom]; testButton.frame = CGRectMake(150, 100, 100, 100); testButton.backgroundColor = [UIColor brownColor]; testButton.layer.cornerRadius = 50; testButton.layer.masksToBounds = YES; [self.view addSubview:testButton]; //用于点击的按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 250, 200, 50); [button setTitle:@"进入下一页" forState:UIControlStateNormal]; button.backgroundColor = [UIColor brownColor]; button.layer.cornerRadius = 6; [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } //按钮的触发事件 - (void)clickButton{ BlockViewController *block = [[BlockViewController alloc]init]; //block的实现 block.buttonBlock = ^(UIImage *image){ [testButton setImage:image forState:UIControlStateNormal]; }; [self presentViewController:block animated:YES completion:nil]; } @end
在自己创建的BlockViewController.h文件中声明block的方法并且把block当属性使用
#import <UIKit/UIKit.h> @interface BlockViewController : UIViewController //block的声明 这里把block当做属性 @property(nonatomic,copy)void(^buttonBlock)(UIImage *image); @end
在BlockViewController.m文件中实现block的回调
#import "BlockViewController.h" @interface BlockViewController () { UIButton *testButton; } @end @implementation BlockViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self backButton]; } - (void)backButton{ testButton = [UIButton buttonWithType:UIButtonTypeCustom]; testButton.frame = CGRectMake(150, 100, 100, 100); [testButton setImage:[UIImage imageNamed:@"头像3.jpg"] forState:UIControlStateNormal]; testButton.backgroundColor = [UIColor brownColor]; testButton.layer.cornerRadius = 50; [self.view addSubview:testButton]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 250, 200, 50); [button setTitle:@"返回上一页" forState:UIControlStateNormal]; button.backgroundColor = [UIColor brownColor]; button.layer.cornerRadius = 6; [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)clickButton{ //block的回调 self.buttonBlock([UIImage imageNamed:@"头像3.jpg"]); //返回到上一页 [self dismissViewControllerAnimated:YES completion:nil]; } @end
时间: 2024-10-02 00:33:02