美文网首页IOS收藏iOS 算法或者代码
Objective-C界面传值(三):Block传值

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

作者: Go_Spec | 来源:发表于2016-04-28 21:29 被阅读467次

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 <UIKit/UIKit.h>

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];
}    

效果如图:

第二页 第一页

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

相关文章

  • iOS_UI_08_界面通信

    第八章 界面通信 一、属性传值 二、协议传值 三、Block传值

  • UI总结-界面传值

    UI总结-界面传值(属性传值,协议传值,block传值,通知中指传值) 在编程过程中,界面传值是很重要的一部分,常...

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

    Block传值 Block,很像C语言中的函数指针,举个例子 不过在使用block时,因为block的声明格式比较...

  • Swift界面传值

    Swift中界面传值的方法 主要有三种 1.代理传值2.闭包传值(即OC中的Block) 属性传值 代理传值 F...

  • OC中反向传值的方法

    oc中反向传值四种方法 block反向传值 在需要传值的界面: 在接受到传值的界面 单例反向传值 创建一个单例类 ...

  • iOS开发-block传值

    block传值步骤比代理传值步骤简便,实际开发中经常使用到。 block传值分为block属性传值和block方法...

  • ios常用的三种传值方式

    iOS中有多种方案可以实现页面之间的传值,例如:属性传值、代理传值、block传值、单例传值...。常用的三种传值...

  • Block传值

    iOS传值一共有四种:属性传值,代理传值,通知传值以及Block传值; 今天我们来说一下Block传值: 概念:带...

  • iOS block简单用法(3)

    block用于传值 效率比代理更高 比如要把A界面的值传给B界面 (1) 定义block A界面的 .h type...

  • oc中使用delegate和block进行反向传值

    delegate传值 block实现传值

网友评论

    本文标题:Objective-C界面传值(三):Block传值

    本文链接:https://www.haomeiwen.com/subject/toyhrttx.html