两种回调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-02-16 16:17:15

    CocoaPods安装和使用教程

    当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等。可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而其他类库又用到其他类库,“子子孙孙无穷尽也”,这也许是比较特殊的情况。总之小编的意思就是,手动一个个去下载所需类库十分麻烦。另外一种常见情况是,你项目中用到的类库有更新,你必须得重新下载新版本,重新加入到项目中,十分麻烦。如果能有什么工具能解决这些恼人的问题,那将“善莫大焉”。所以,你需要 CocoaPods。

  • 2020-02-16 17:13:34

    iOS优秀Objective-C开源库集锦

    自己从事iOS开发工作接近两年左右的时间了,在自己工作之余,收集整理了一些优秀的三方开源框架,自己整理的这些三方开源库涵盖了iOS开发面很多方面的知识。非常感谢这些开源库的作者们,正是因为这些库,提高了我们的开发效率,同样也是我们学习进步的源泉。现将这个整理工程文件分享出来,希望能给需要的朋友一些帮助,同时也自己也做下收集记录。

  • 2020-02-19 23:04:52

    理解Laravel中的pipeline

    pipeline在laravel的启动过程中出现次数很多,要了解laravel的启动过程和生命周期,理解pipeline就是其中的一个关键点。网上对pipeline的讲解很少,所以我自己写一写吧。