Objective-C界面传值(三):Block传值

Block传值

Block,很像C语言中的函数指针,举个例子

int (*)(int a, int b)//C语言中函数指针的类型
int (*p)(int a, int b) = NULL;//一个完整的函数指针
 
NSInteger(^)(NSInteger a, NSInteger b)//代码块类型
NSInteger(^MyBlock)(NSInteger a, NSInteger b)//声明了一个名为MyBlock代码块

不过在使用block时,因为block的声明格式比较复杂,通常将其进行重定义:

typedef void(^StringBlock)(NSString *string);

*这样就可以使用StringBlock来声明void(^)(NSString string)类型.

下面来看一下代码实现:

AppDelegate.m的实现过程在这里不在重复给出,具体步骤可以到我的上篇文章里阅读:(Objective-C界面传值(一):属性传值)

ViewController.m

- (void)dealloc
{
    [super dealloc];
}

- (void)loadView
{
    [super loadView];

    self.textField             = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 175, 50)];
    self.textField.placeholder = @"请输入文本";
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
     [self.view addSubview:_textField];
    [_textField release];

    UIBarButtonItem *rightButton           = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(didClickedRightBarButton:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    [rightButton release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"首页";
    self.navigationController.navigationBar.barStyle    = UIBarStyleBlack;
    self.navigationController.navigationBar.translucent = NO;
}

#pragma mark 按钮点击方法
- (void)didClickedRightBarButton:(UIBarButtonItem *)button
{
    SecondViewController *vc2 = [[SecondViewController alloc] init];
    vc2.secondString = self.textField.text;
    vc2.block = ^(NSString *string)
    {
        self.textField.text = string;//在这里,我们声明了SecondViewController的对象vc2,那我们就可以调用vc2的StringBlock类型的属性.将我们要用的物品拿出来进行使用.
    };
    [self.navigationController pushViewController:vc2 animated:YES];
    [vc2 release];
}

SecondViewController.h

#import 

typedef void(^StringBlock)(NSString *string);//重定义block

@interface SecondViewController : UIViewController

@property(nonatomic, copy) NSString *secondString;
@property(nonatomic, copy) StringBlock block;

@end

SecondViewController.m

- (void)dealloc
{
    [super dealloc];
}

- (void)loadView
{
    [super loadView];

    self.textField             = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 175, 50)];
    self.textField.placeholder = @"请输入文本";
    self.textField.text        = self.secondString;
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textField];
    [_textField release];

    UIBarButtonItem *leftButton           = [[UIBarButtonItem alloc]     initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(didClickedLeftBarButton:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    [leftButton release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"第二页";
}

#pragma mark 按钮点击方法
- (void)didClickedLeftBarButton:(UIBarButtonItem *)button
{
    self.block(self.textField.text);//在按钮的点击方法中将文本框中的值放入block,执行的操作就是将文本框中的字符串赋给block的参数string.这里可以理解为将物品进行装箱操作,什么时候需要用到了,在把要用的物品取出来进行使用.
    [self.navigationController popViewControllerAnimated:YES];
}    

效果如图:

Objective-C界面传值(三):Block传值_第1张图片
第二页
Objective-C界面传值(三):Block传值_第2张图片
第一页

总结:Block通常用于从后向前传值,个人觉得比起协议传值来说步骤更简单,而且Xcode中重定义代码是有代码块的,用起来很方便.在OC中,block的功能特别多,在以后的文章中也会介绍到.界面传值这三篇文章的技术点都很基础,但是实用性非常高,是重中之重.如我个人的理解如果有不到位的地方,欢迎交流及指正.

你可能感兴趣的:(Objective-C界面传值(三):Block传值)