iOSBlock回调

在第二个页面的.h中

#import <UIKit/UIKit.h>
//块语法回传第一步,声明
typedef void(^ShowTextBlock)(NSString *text);
@interface NextViewController : UIViewController
//快语法回传第二步,声明块类型的对象
@property(nonatomic,copy) ShowTextBlock showTextBlock;
@end

在第二个页面的.m中

#import "NextViewController.h"

@interface NextViewController ()

@end

@implementation NextViewController
- (IBAction)clickReturn:(UITextField *)sender {
//块语法回传第三步,适当的位置调用
    _showTextBlock(sender.text);
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

在第一个页面的.h中获取:

#import "MyViewController.h"
#import "NextViewController.h"
@interface MyViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation MyViewController
- (IBAction)click:(id)sender {
    NextViewController *nextVC = [NextViewController new];
//第四步:获取到回调信息.
    nextVC.showTextBlock = ^(NSString *text){
        _label.text = text;
    };
    [self presentViewController:nextVC animated:YES completion:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,990评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,107评论 19 139
  • 文/夏莲 荒无人烟的蛮荒之地 一棵草却能够无声无息 风沙漫漫的大沙漠里 一株仙人掌亦能无坚不摧 冰天雪地的高原上 ...
    周小锦阅读 398评论 40 8
  • 粒粒尘埃 沉入空气 低压 昏迷 阳光照进回忆里 似低语 似哭泣 叹 世界崩塌 探 你的底细 说吧 将你所有彷徨失意...
    Aprilme阅读 546评论 0 2
  • 1 我们从认识到订婚只用了三天的时间,而我们从订婚到得知我怀孕也只用了三个多月的时间。一切都来得很匆忙,匆忙到我们...
    百凌爱读书阅读 226评论 0 0