码迷,mamicode.com
首页 > 其他好文 > 详细

[Objective-C] 020_ Block

时间:2015-12-28 01:00:02      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

1.定义和使用Block

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 
 5 @end
 6 
 7 @implementation ViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     
12     //定义block 无返回值,无参数
13     void (^nameBlock)() = ^ () {
14         NSLog(@"姓名: Block");
15     };
16     
17     //定义block 有返回值,有参数
18     int (^ageBlock)(int) = ^(int age) {
19         NSLog(@"年龄: %d",age);
20         return age + 1;
21     };
22     
23     //调用block
24     nameBlock();
25     int age = ageBlock(3);
26     NSLog(@"》》》年龄:%d",age);
27     nameAndAgeBlock(@"SD.Team",2015);
28 }
29 
30 void (^nameAndAgeBlock)() = ^(NSString *name,int age) {
31     NSLog(@"姓名:%@,年龄:%d",name,age);
32 };
33 
34 - (void)didReceiveMemoryWarning {
35     [super didReceiveMemoryWarning];
36 }
37 
38 @end
39 
40 Block 定义与使用

  运行结果:
  技术分享
  通过运行上面的简单代码示例,可以得知:
    [1].在类中,定义一个Block变量,就像定义一个函数。
    [2].Block可以定义在方法内部,也可以定义在方法外部。
    [3].只有调用Block时候,才会执行其{}体内的代码。

2.__block关键字

  在Block的{}体内,是不可以对外面的变量进行更改的,将会报错(Variable is not assigning (missing __block type)),比如下面:

1 - (void)viewDidLoad {
2     [super viewDidLoad];
3     int myAge = 25;
4     void (^updateAge)(int) = ^(int age){
5         myAge = myAge + age;
6         NSLog(@"age:%d",myAge);
7     };
8 }

  要如何更正才能对外面的值呢?通过添加__block 关键字即可

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     __block int myAge = 25;
 4     void (^updateAge)(int) = ^(int age){
 5         myAge = myAge + age;
 6         NSLog(@"age:%d",myAge);
 7     };
 8     
 9     updateAge(3);
10 }


3.Block作为property属性

  如有一需求:在ViewController中,点击设置按钮,push到下一个页面SettingViewController,在SettingViewController的age输入框TextField中更新年龄,返回的时候,在ViewController的年龄Label上面显示已更新的年龄。可以通过delegate 来实现,delegate前面讲过了,这次我们就用block来实现.
  SettingViewController:

 1 //SettingViewController.h 文件
 2 @interface SettingViewController : UIViewController
 3 @property (nonatomic, copy) void (^updateAgeBlock)(NSString *age);
 4 
 5 @end
 6 
 7 //SettingViewController.m 文件
 8 - (IBAction)updateAgeBtnClicked:(id)sender {
 9     if (self.updateAgeBlock) {
10         self.updateAgeBlock(self.ageTextField.text);
11     }
12     [self.navigationController popViewControllerAnimated:YES];
13 }

  ViewController:

 1 - (IBAction)settingClicked:(id)sender
 2 {
 3     SettingViewController *settingVC = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
 4     settingVC.updateAgeBlock = ^(NSString *age){
 5         [self updateAgeLabel:age];
 6     };
 7     [self.navigationController pushViewController:settingVC animated:YES];
 8 }
 9 
10 - (void)updateAgeLabel:(NSString *)age
11 {
12     self.ageLabel.text = age;
13 }

我们通过block方式同样达到了delegate的效果。

[Objective-C] 020_ Block

标签:

原文地址:http://www.cnblogs.com/superdo/p/5081115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!