Block入門,mblock入門

來源:互聯網
上載者:User

Block入門,mblock入門

       iOS4.0開始,Block橫空出世,它其實就是c預言的補充,書面點說就是帶有自動變數的匿名函數,Block簡潔,代碼的可讀性也高,因此深受廣大開發人員的喜愛,這一次給大家介紹Block的基本類型和項目中的實際操作。

Block的形式如下:

Block的基本類型1.無參數無傳回值
void(^tempBlock)() = ^(){        NSLog(@"無參無傳回值");    };//調用tempBlock();

 

2.無參數有傳回值
int(^tempBlock)() = ^(){        return 10;    };//調用的時候,無論你輸入的是什麼都返回的是10;tempBlock(100);

 

3.有參數無傳回值
void(^tempBlock)(int) = ^(int temp){        NSLog(@"有參數無傳回值"); };

 

4.有參數有傳回值
int(^tempBlock)(int) = ^(int number){        return number;    };//輸入多少列印就是多少tempBlock(100);

 

Block的經典實用情境1.修改外部變數
__block int x = 100;void(^sumXWithYBlock)(int) = ^(int y){      x = x + y;      NSLog(@"new value %d",x); };//列印的值就是x+y,100+100=200sumXWithYBlock(100);

 

2.頁面間的傳值
//1.在第二個頁面(SecondViewController)首先聲明一個屬性/**先聲明block的名字,並確定參數的類型*/@property(nonatomic,copy)void (^netViewBlock)(NSString *text);//2.在點擊按鈕返回的時候,往回傳你需要傳的參數,參數類型要一致-(void)back{  self.netViewBlock(@"你好");  [self.navigationController popViewControllerAnimated:YES];}

 

3.在第一頁(FirstViewController),準備push進入下一頁的時候,擷取ViewController2的屬性,並實現。
-(void)click:(UIButton *)sender{//把第二頁的返回的值顯示在label上  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 100, 200, 30)];  [self.view addSubview:label];  SecViewController *vc = [[SecViewController alloc] init];  vc.netViewBlock = ^(NSString *text){      label.text = text;  };  [self.navigationController pushViewController:vc animated:YES];}

 

自訂Block

例子:點擊Button,需要改變Button的title

實現:

1.建立一個工具類,聲明一個類方法,並自訂一個block,需要傳title,所以傳參類型是NSString

@interface ChangeBuTitleTool : NSObject+(void)changeBuTitleWithText:(void(^)(NSString *titleText))text;@end

2.實現

@implementation ChangeBuTitleTool+(void)changeBuTitleWithText:(void(^)(NSString *titleText))text{  if (text) {      text(@"tyler");  }}@end

3.在控制器裡Button的點擊的時候,實現改變title的方法

-(void)addButton{  UIButton *bu = [UIButton buttonWithType:(UIButtonTypeCustom)];  bu.backgroundColor = [UIColor blueColor];  bu.frame = CGRectMake(30, 90, 100, 50);  [self.view addSubview:bu];  [bu addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];    }-(void)click:(UIButton *)sender{  [ChangeBuTitleTool changeBuTitleWithText:^(NSString *titleText) {      [sender setTitle:titleText forState:(UIControlStateNormal)];  }]; }

4.Block與typedef的結合

   在上一個例子中,聲明一個類方法,其中定義block直接寫在類方法裡,看起來很不和諧,尤其是對新手看起來可讀性不太高,可以用typedef單獨定義一個block,增加代碼的可讀性。

typedef void (^titleBlock)(NSString *titleText);@interface ChangeBuTitleTool : NSObject+(void)changeBuTitleWithText:(titleBlock)text;//+(void)changeBuTitleWithText:(void(^)(NSString *titleText))text;@end@implementation ChangeBuTitleTool+(void)changeBuTitleWithText:(titleBlock)text{  if (text) {      text(@"tyler");  }}@end

 

Block入門篇就介紹到這裡,下期更精彩!<( ̄︶ ̄)>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.