两种回调delegate和block的方式实现

2021-04-19 10:54:16

参考地址 IOS:两种回调的方式实现(delegate和block)


通过block回调,可以轻松实现类似android的 setListener这样的机制,记得使用弱引用哦

我要实现的效果:在ViewController里有一个Button,点击button进入BlockView。在BlockView有一个TextField,输入文字后点击完成,可以把textField里的文字回调到ViewController的Button上显示。


第一种:delegate回调


回调类ViewController.m:
#import "ViewController.h"
#import "BlockView.h"

@interface ViewController ()<BlockViewDelegate>
@property (strong, nonatomic) UIButton *myButton;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.myButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 60, 30)];
    self.myButton.backgroundColor = [UIColor redColor];
    [self.myButton addTarget:self action:@selector(doButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.myButton];
}

- (void)doButtonAction{
    BlockView *blockView = [[BlockView alloc] init];
    blockView.delegate = self;
    [self.navigationController pushViewController:blockView animated:YES];
}

- (void)returnFieldText:(NSString *)string{
    [self.myButton setTitle:string forState:UIControlStateNormal];
}

调用类BlockView.h

#import <UIKit/UIKit.h>
@protocol BlockViewDelegate

- (void)returnFieldText:(NSString *)string;

@end
@interface BlockView : UIViewController
@property (assign,nonatomic) id<BlockViewDelegate> delegate;
@end

调用类BlockView.m

#import "BlockView.h"
@interface BlockView ()
@property (strong, nonatomic) UITextField *textField;
@end
@implementation BlockView

- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
    self.textField.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.textField];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doRightButtonAction)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void)doRightButtonAction{
    [self.delegate returnFieldText:self.textField.text];
    [self.navigationController popViewControllerAnimated:YES];
}



第二种:block回调


回调类ViewController.m:

#import "ViewController.h"
#import "BlockView.h"

@interface ViewController ()
@property (strong, nonatomic) UIButton *myButton;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.myButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 60, 30)];
    self.myButton.backgroundColor = [UIColor redColor];
    [self.myButton addTarget:self action:@selector(doButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.myButton];
}

- (void)doButtonAction{
    BlockView *blockView = [[BlockView alloc] init];
    
    __weak typeof (self)weakSelf = self;
    //回调block
    [blockView returnTextFieldWithBlock:^(NSString *text) {
        [weakSelf.myButton setTitle:text forState:UIControlStateNormal];
    }];
    [self.navigationController pushViewController:blockView animated:YES];
}


调用类BlockView.h

#import <UIKit/UIKit.h>

typedef void (^ReturnTextWithBlock)(NSString *text);
@interface BlockView : UIViewController
@property (copy ,nonatomic) ReturnTextWithBlock returnTextWithBlock;

- (void)returnTextFieldWithBlock:(ReturnTextWithBlock)block;
@end

调用类BlockView.m

#import "BlockView.h"
@interface BlockView ()
@property (strong, nonatomic) UITextField *textField;
@end
@implementation BlockView

- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
    self.textField.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.textField];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doRightButtonAction)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void)doRightButtonAction{
    self.returnTextWithBlock(self.textField.text);
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)returnTextFieldWithBlock:(ReturnTextWithBlock)block{
    self.returnTextWithBlock = block;
}


  • 2020-01-16 08:52:22

    Vue函数式调用组件创建公共组件

    所有组件都需要这么去调用,就会有些许麻烦而且不太美观。像Loading、Toast等这些组件,一页面可以经常用到而且每次显示的内容都可能不一样,这样的话用js的方式【this.$xxx.show(option)】去调用就方便很多,而且代码也更整洁。

  • 2020-01-17 08:37:26

    css transition分别指定多个属性

    transition有四个属性,很多人都会遗忘,分别是transition-property,transition-duration,transition-timing-function,transition-delay,尤其是transition-delay,这个可以实现延迟动画

  • 2020-01-17 08:44:57

    vue keepalive 前进刷新后退不刷新终极解决方案

    另外,我们做路由的时候要有意的根据页面等级做出路由的长度 比如 /a是一级的页面/a/b是二级的页面,下面的文章大家也可以通过判断path的长度来计算rank值,不用有意自定了 这样做的好处有两点,一个就是前进刷新,后退不刷新,还有就是,如果我们做页面进出效果的时候也能排上用场。

  • 2020-01-17 15:28:24

    深入理解vue中的slot与slot-scope

    vue中关于插槽的文档说明很短,语言又写的很凝练,再加上其和methods,data,computed等常用选项使用频率、使用先后上的差别,这就有可能造成初次接触插槽的开发者容易产生“算了吧,回头再学,反正已经可以写基础组件了”,于是就关闭了vue说明文档。

  • 2020-01-17 21:20:06

    Nuxt重要点介绍和记录

    nuxt.js简单的说是Vue.js的通用框架,最常用的就是用来作SSR(服务器端渲染).Vue.js是开发SPA(单页应用)的,Nuxt.js这个框架,用Vue开发多页应用,并在服务端完成渲染,可以直接用命令把我们制作的vue项目生成为静态html。

  • 2020-01-17 21:21:42

    nuxtjs打包优化

    然后你再使用 npm run build 打包的时候,会弹出一个界面 当然了,它第一次出来的时候我立马关了,这啥玩意???别着急你慢慢的品,慢慢的看。 原来 这是打包后的JS,面积的大小就是体积的大小。这样我们一目了然的看到了到底那里大了