首页 > 代码库 > block的简单使用
block的简单使用
一: 定义block 初始化
#import <UIKit/UIKit.h>
@interface LHQDemoView : UIView
- (instancetype)initWithFrame:(CGRect)frame andCompelete:(void(^)(NSString *msg))block;
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c42275 } span.s1 { } span.s2 { color: #c42275 } span.s3 { color: #703daa } span.s4 { color: #6122ae } span.s5 { color: #3e1e81 } span.s6 { color: #0435ff } span.s7 { color: #000000 } span.s8 { color: #c81b13 } span.s9 { font: 18.0px "PingFang SC"; color: #c81b13 } span.s10 { color: #539aa4 }</style>
- (instancetype)initWithFrame:(CGRect)frame andCompelete:(void(^)(NSString *msg))block{
if(self = [super initWithFrame:frame]){
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[btn setTitle:@"提示" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
self.block = block;
}
return self;
}
二: 定义全局block
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3c828b } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421; min-height: 21.0px } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c42275 } span.s1 { color: #c42275 } span.s2 { color: #000000 } span.s3 { } span.s4 { font: 18.0px "PingFang SC" } span.s5 { font: 18.0px Menlo } span.s6 { color: #6122ae }</style>@interface LHQDemoView()
//block定义的时候一定要用copy
/*
block默认在栈中 栈中内存归系统管理
系统管理有个弊端:到作用于结束就被干掉
执行了一个copy操作之后,就会把block从栈中放到堆中
会自动有一个强引用来指向它
*/
@property(nonatomic,copy)void(^block)(NSString *);
设置
self.block = block;
三: 给block赋值
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } span.s1 { } span.s2 { color: #c42275 } span.s3 { color: #6122ae } span.s4 { color: #000000 } span.s5 { color: #539aa4 } span.s6 { font: 18.0px "PingFang SC" } span.s7 { color: #3e1e81 }</style>- (void)btnClicked: (UIButton *)btn{
self.block(@"点击了某个按钮");
NSLog(@"btnClicked");
}
四: 使用
导入头文件
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3c828b } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c42275 } span.s1 { color: #822d0f } span.s2 { } span.s3 { color: #c42275 } span.s4 { color: #000000 }</style>#import "LHQDemoView.h"
@interface ViewController ()
@end
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } span.s1 { } span.s2 { color: #3c828b } span.s3 { color: #3e1e81 } span.s4 { color: #294c50 } span.s5 { color: #0435ff } span.s6 { color: #6122ae } span.s7 { color: #000000 } span.s8 { color: #c81b13 } span.s9 { font: 18.0px "PingFang SC"; color: #c81b13 } span.s10 { color: #c42275 } span.s11 { color: #703daa }</style>
LHQDemoView *demoView = [[LHQDemoView alloc]initWithFrame:CGRectMake(100, 200, 100, 100) andCompelete:^(NSString *msg) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
[alert show];
}];
[self.view addSubview:demoView];
block的简单使用