两种回调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;
}


  • 2019-11-18 23:18:49

    spring boot中读取配置信息一

    首先我们都知道一个常识,那就是每个人都有自己的年龄,比如我们现在的业务需求是查询所有年龄大于20的人的相关信息,如果我们选择通过配置文件来配置这个值为20的常量的话,我们该如何配置和如何从配置文件中获取这个值呢?,application.yml的内容如下(注意 “age:“ 和 “20“ 之间需要一个空格,yml的语法 ):

  • 2019-11-19 01:20:18

    java8 forEach、filter、map

    filter()、findAny()、orElse()配合使用,可以根据条件获取某个元素,如果没有返回指定的值。

  • 2019-11-19 01:24:01

    使用JAVA8 filter对List多条件筛选

    记录项目开发的过程中遇到的一些问题及解决方法,由于公司操作数据库都是统一使用工具生成的存在一些多表查询模糊查询,这些操作只能在集合方面下手了,比如发送邮件记录方面查询,对用户的名字及邮件模糊检索 年龄匹配查询。

  • 2019-11-21 18:13:08

    如何在vue单页应用中使用百度地图

    百度开发者平台已经封装了基于vue的地图组件,详细使用,请参考官网: https://dafrok.github.io/vue-baidu-map/#/zh/start/installation 网上有一些是直接在index.html页面全部引用的,本人强烈反对此种使用方式,因为我们项目是组件化的单页应用,强行引入多页应用的开发方式,会破坏整个项目的框架,严重影响性能。有些甚至还在vue单页应用中引入jquery,感觉这都是一些反人类的骚操作,不到万不得已,不建议使用。