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,floatt){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
- @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
- @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