星空网 > 软件开发 > 操作系统

block 代码块 反向传值

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

 




原标题:block 代码块 反向传值

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

海外版tiktok官网入口:https://www.goluckyvip.com/tag/81146.html
tiktok 官网入口:https://www.goluckyvip.com/tag/81147.html
tiktok色板:https://www.goluckyvip.com/tag/81148.html
tiktok怎么在国内看:https://www.goluckyvip.com/tag/81149.html
tiktok海外国际版官网:https://www.goluckyvip.com/tag/81150.html
外国版tiktok下载:https://www.goluckyvip.com/tag/81151.html
独家丨B站广告位可跳转美团APP B站为电商平台引流再升级 :https://www.kjdsnews.com/a/1836410.html
百崖大峡谷生态旅游景区(探秘中国西南自然风光):https://www.vstour.cn/a/363176.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流