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


  • 2023-08-19 18:57:08

    强制杀死nginx进程简单粗暴

    nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory),最简单的办法,杀死nginx进程,重启