IOS 强大的Block
来源:互联网 发布:js全局数组变量 编辑:程序博客网 时间:2024/06/05 10:01
这个block,在很多框架里有好到,还在GCD里大方光彩,我们只要清晰的掌握它的语法和使用方法就行了。感觉他就相当于Java的接口函数,可以把声明和定义分割开来达到抽象逻辑和具体代码分离的效果。
Block,简单地说就是传递代码块。
想要掌握它,首先掌握它的语法。
上面这个张图片就说的很清楚,然后我再举三个例子,大家再结合着理解
/** * 无参数无返回值的Block */-(void)oneClick{ /** * void :就是无返回值 * emptyBlock:就是该block的名字 * ():这里相当于放参数。由于这里是无参数,所以就什么都不写 */ void (^emptyBlock)() = ^(){ NSLog(@"无参数,无返回值的Block"); }; emptyBlock();}- (void)twoClick{ /** * 调用这个block进行两个参数相加 * * @param int 参数A * @param int 参数B * * @return 无返回值 */ void (^sumBlock)(int ,int ) = ^(int a,int b){ NSLog(@"%d + %d = %d",a,b,a+b); }; /** * 调用这个sumBlock的Block,得到的结果是20 */ sumBlock(10,10);}- (void)threeClick{ /** * 有参数有返回值 * * @param NSString 字符串1 * @param NSString 字符串2 * * @return 返回拼接好的字符串3 */ NSString* (^logBlock)(NSString *,NSString *) = ^(NSString * str1,NSString *str2){ return [NSString stringWithFormat:@"%@%@",str1,str2]; }; //调用logBlock,输出的是 我是Block NSLog(@"%@", logBlock(@"我是",@"Block"));}
这还只是基本使用,平常我们使用的时候都是通过typedef来在两个ViewController来传递代码块。说白了,就是BViewController声明block,AViewController在跳转到BViewController的时候去定义这个block。例子代码如下:
AViewController- (void)fourClick{ TwoViewController *vc =[[TwoViewController alloc]init]; vc.str_nslog = ^(NSString *str){ NSLog(@"%@",str); }; [self presentViewController:vc animated:YES completion:nil];}
BVIewController#import <UIKit/UIKit.h>@interface TwoViewController : UIViewControllertypedef void(^nslog)(id);/** * 用上面定义的changeColor声明一个Block,声明的这个Block必须遵守声明的要求。 */@property (nonatomic, copy) nslog str_nslog;@end//// TwoViewController.m// BmobTest//// Created by shanreal-iOS on 2017/12/21.// Copyright © 2017年 shanreal.LongZhenHao. All rights reserved.//#import "TwoViewController.h"@interface TwoViewController ()@property(nonatomic,strong)UIButton* one;@end@implementation TwoViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self.view setBackgroundColor:[UIColor whiteColor]]; _one = [[UIButton alloc]initWithFrame:CGRectMake(20, 20, 100, 30)]; [_one setTitle:@"one" forState:UIControlStateNormal]; [_one setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; _one.font = [UIFont systemFontOfSize:20]; [_one addTarget:self action:@selector(oneClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_one];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/-(void)oneClick{ NSString* str = @"传递代码块成功"; self.str_nslog(str);}@end
阅读全文
0 0
- IOS 强大的Block
- iOS 强大的URLProtocol
- iOS block的用法
- iOS block的用法
- iOS block的用法
- iOS block的用法
- IOS的Block
- iOS-block的总结
- iOS block的用法
- iOS block的用法
- iOS Block的语法
- iOS block的用法
- iOS block的用法
- iOS block的使用
- ios block 的使用
- iOS block的用法
- iOS block的用法
- iOS block的用法
- gouwuche的application
- 数据结构实验之查找四:二分查找
- model中的bean
- clickhouse之mergetree详解
- Python学习-list 和 tuple
- IOS 强大的Block
- python与自然语言处理之朴素贝叶斯上
- node.js Express框架入门
- R语言并行运算(snow包)
- bash cookbook
- 值得学习的C++开源项目(转)
- cocos layer触摸事件不响应的问题
- mysql 日期函数to_days注意事项
- python与自然语言处理之贝叶斯实战