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,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還可以用於多線程,這個以後再說

   

  1. #import   
  2.   
  3. @interface BIDViewController : UIViewController  
  4. - (IBAction)clicked:(UIButton *)sender;  
  5. - (IBAction)secondClicked:(id)sender;  
  6. - (IBAction)thirdClicked:(id)sender;  
  7. @property (retain, nonatomic) IBOutlet UILabel *lb;  
  8.   
  9. @end  

 

 

  1. #import "BIDViewController.h"  
  2. #import "FirstViewController.h"  
  3. @interface BIDViewController ()  
  4. {  
  5.     float (^oneFrom)(float);  
  6. }  
  7. @end  
  8.   
  9. @implementation BIDViewController  
  10.   
  11. - (void)viewDidLoad  
  12. {  
  13.     [super viewDidLoad];  
  14.     // Do any additional setup after loading the view, typically from a nib.  
  15.     oneFrom=^(float aFloat){  
  16.         float result=aFloat-1.0;  
  17.         return result;  
  18.     };  
  19. }  
  20.   
  21. - (void)didReceiveMemoryWarning  
  22. {  
  23.     [super didReceiveMemoryWarning];  
  24.     // Dispose of any resources that can be recreated.  
  25. }  
  26.   
  27. - (IBAction)clicked:(UIButton *)sender {  
  28.     NSLog(@"%f",oneFrom(10.0));  
  29.       
  30.       
  31.     void(^thirdfrom)(float)=^(float r){  
  32.         NSLog(@"%f",r*5.0);  
  33.     };  
  34.     [self changeValue:thirdfrom];  
  35. }  
  36.   
  37. - (IBAction)secondClicked:(id)sender {  
  38.     __block CGFloat height=10.0;  
  39.     void(^twoFrom)(float)=^(float t){  
  40.         height+=t;  
  41.     };  
  42.     NSLog(@"height is %f",height);  
  43.     twoFrom(5.0);  
  44.      NSLog(@"height  is %f",height);  
  45. }  
  46.   
  47. - (IBAction)thirdClicked:(id)sender {  
  48.     FirstViewController* first=[[FirstViewController alloc] init];  
  49.     __block UILabel* blockLb=_lb;  
  50.     //回調  
  51.     first.testBlock=^(float r){  
  52.         blockLb.text=[NSString stringWithFormat:@"%f",r];  
  53.     };  
  54.     [self presentModalViewController:first animated:YES];  
  55. }  
  56.   
  57. -(void)changeValue:(void(^)(float))from  
  58. {  
  59.     [NSThread sleepForTimeInterval:3.0];  
  60.     from(10.0);  
  61. }  
  62. -(void)dealloc  
  63. {  
  64.   
  65.     [_lb release];  
  66.     [super dealloc];  
  67. }  
  68. - (void)viewDidUnload {  
  69.     [self setLb:nil];  
  70.     [super viewDidUnload];  
  71. }  
  72. @end  

  1. #import   
  2.   
  3. @interface FirstViewController : UIViewController  
  4. @property(nonatomic,copy)void(^testBlock)(float);  
  5. - (IBAction)btnClicked:(id)sender;  
  6.   
  7. @end  

  1. #import "FirstViewController.h"  
  2.   
  3. @interface FirstViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation FirstViewController  
  8. @synthesize testBlock;  
  9. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  10. {  
  11.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  12.     if (self) {  
  13.         // Custom initialization  
  14.     }  
  15.     return self;  
  16. }  
  17.   
  18. - (void)viewDidLoad  
  19. {  
  20.     [super viewDidLoad];  
  21.     // Do any additional setup after loading the view from its nib.  
  22. }  
  23.   
  24. - (void)didReceiveMemoryWarning  
  25. {  
  26.     [super didReceiveMemoryWarning];  
  27.     // Dispose of any resources that can be recreated.  
  28. }  
  29.   
  30. - (void)dealloc {  
  31.     [super dealloc];  
  32. }  
  33. - (void)viewDidUnload {  
  34.     [super viewDidUnload];  
  35. }  
  36. - (IBAction)btnClicked:(id)sender {  
  37.     if (testBlock) {  
  38.         testBlock(4.0);  
  39.     }  
  40.     [self dismissModalViewControllerAnimated:YES];  
  41. }  
  42. @end  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章