之前程序中一直用block这里把它进行归类一下。block是ios4.0之后出来的一门新技术,应用也非常广泛,只要我们撑握其使用的翘门,使 用其来也是非常简单的,而且它能将传统程序简单化
程序中" ^ "代表程序块也就是block,在我们接下来的程序中都会有^
block的声明一般都是 返回类型(^block名称)(参数,...,...) 如 void(^firstBlock)(float ,float);
block的定义 可以是如下 firstBlock=^(float r,float t){ NSLog(@"value is %f",r*t };
我们也可以将声明和定义放在一块实现如下:
void(^firstBlock)(float,float)=^(float r,float t){NSLog(@"value is %f",r*t};
当我们需要使用它的时候执行如下 firstBlock(5.0,6.0);
在这里需要注意一下,我们使用它的时候应该在能使用其范围内才能使用它,就跟变量一样,只有在其作用域 内才能使用它,写到这里觉得block跟变量差不多,说到跟变量一样这里提一下
就是block引用外部的非对象型变量的时候一般是不能更改它的,除非在变量前加上__block,对于对象型的变量在block的内部引用的时候一般引用计数都会加1,为了打破这种 retain circle,可以在对象前加__block,这样block块就不会维护这个对象了
block不仅能单独使用,还可以作为方法的参数如下
-(void)changeValue:(void(^)(float,float))block,还过这里的block的写法和上面不一样,请注意
使用也很简单 [self changeValue:firstBlock];
再就是block还可以代替delegate进行回调,如有A和B两个UIViewController,给B定义一个属性block,从A界面到B中时设置B的block,当我们需要在B中的任何时候执行回调的时候,直接执行block就行了,说到这里就不得不说一下作为属性block是不能进行retain的只能进行copy.还有block还可以用于多线程,这个以后再说
#import <UIKit/UIKit.h>
@interface BIDViewController : UIViewController
- (IBAction)clicked:(UIButton *)sender;
- (IBAction)secondClicked:(id)sender;
- (IBAction)thirdClicked:(id)sender;
@property (retain, nonatomic) IBOutlet UILabel *lb;
@end
#import "BIDViewController.h"
#import "FirstViewController.h"
@interface BIDViewController ()
{
float (^oneFrom)(float);
}
@end
@implementation BIDViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
oneFrom=^(float aFloat){
float result=aFloat-1.0;
return result;
};
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)clicked:(UIButton *)sender {
NSLog(@"%f",oneFrom(10.0));
void(^thirdfrom)(float)=^(float r){
NSLog(@"%f",r*5.0);
};
[self changeValue:thirdfrom];
}
- (IBAction)secondClicked:(id)sender {
__block CGFloat height=10.0;
void(^twoFrom)(float)=^(float t){
height+=t;
};
NSLog(@"height is %f",height);
twoFrom(5.0);
NSLog(@"height is %f",height);
}
- (IBAction)thirdClicked:(id)sender {
FirstViewController* first=[[FirstViewController alloc] init];
__block UILabel* blockLb=_lb;
//回调
first.testBlock=^(float r){
blockLb.text=[NSString stringWithFormat:@"%f",r];
};
[self presentModalViewController:first animated:YES];
}
-(void)changeValue:(void(^)(float))from
{
[NSThread sleepForTimeInterval:3.0];
from(10.0);
}
-(void)dealloc
{
[_lb release];
[super dealloc];
}
- (void)viewDidUnload {
[self setLb:nil];
[super viewDidUnload];
}
@end
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@property(nonatomic,copy)void(^testBlock)(float);
- (IBAction)btnClicked:(id)sender;
@end
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
@synthesize testBlock;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[super dealloc];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (IBAction)btnClicked:(id)sender {
if (testBlock) {
testBlock(4.0);
}
[self dismissModalViewControllerAnimated:YES];
}
@end
有什么问题请多多指教