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


  • 2018-12-09 22:50:46

    Toolbar 标题字体大小及字体颜色

    无师无门遇到点破事也得百度,就单单这破问题 Toolbar 标题字体大小及字体颜色,结果百度一推重复没用的结果,要么一推英文的解释,小学学历的我表示很崩溃!

  • 2018-12-10 00:57:37

    Android沉浸式状态栏(透明状态栏)最佳实现

    在Android4.4之前,我们的应用没法改变手机的状态栏颜色,当我们打开应用时,会出现上图中左侧的画面,在屏幕的顶部有一条黑色的状态栏,和应用的风格非常不协调;为了提供更好的界面交互,google在Android4.4以后提供了设置沉浸式状态栏的方法,对于沉浸式状态栏这个名字存在争议,我们不做讨论,实际的效果其实就是透明的状态栏,然后在状态栏的位置显示我们自定义的颜色,通常为应用的actionbar的颜色,或者是将应用的整体的一张图片也占据到状态栏中,如下图所示:

  • 2018-12-11 10:20:40

    Android下载图片到相册

    调用以上系统自带的方法会把bitmap对象保存到系统图库中,但是这种方法无法指定保存的路径和名称,上述方法的title、description参数只是插入数据库中的字段,真实的图片名称系统会自动分配。 或者

  • 2018-12-11 15:45:00

    Laravel中七个非常有用但很少人知道的Carbon方法

    在编写PHP应用时经常需要处理日期和时间,Carbon继承自 PHP DateTime 类的 API 扩展,它使得处理日期和时间更加简单,这篇文章主要给大家分享了Laravel中七个非常有用但很少人知道的Carbon方法,需要的朋友可以参考下。

  • 2018-12-13 11:41:23

    Android drawable微技巧,你所不知道的drawable的那些细节

    好像有挺久时间没更新博客了,最近我为了准备下一个系列的博客,也是花了很长的时间研读源码。很遗憾的是,下一个系列的博客我可能还要再过一段时间才能写出来,那么为了不至于让大家等太久,今天就给大家更新一篇单篇的文章,讲一讲Android drawable方面的微技巧。

  • 2018-12-13 17:14:41

    Android安全开发之浅谈密钥硬编码

    在阿里聚安全的漏洞扫描器中和人工APP安全审计中,经常发现有开发者将密钥硬编码在Java代码、文件中,这样做会引起很大风险。信息安全的基础在于密码学,而常用的密码学算法都是公开的,加密内容的保密依靠的是密钥的保密,密钥如果泄露,对于对称密码算法,根据用到的密钥算法和加密后的密文,很容易得到加密前的明文;对于非对称密码算法或者签名算法,根据密钥和要加密的明文,很容易获得计算出签名值,从而伪造签名。