iOS 关于页面回调传值的总结(delegete、block)

应用方式为:

控制器A中有一个按钮,点击跳转至控制器B,控制器B中有多个按钮,点击不同的按钮,返回给A不同的内容。


1.block方式


A.m

#import "ViewControllerA.h"
#import "ViewControllerB.h"

@interface ViewControllerA ()

@property (nonatomic, strong) ViewControllerB *controllerB;

@property (nonatomic, strong) UILabel *testLabel;

@end

@implementation ViewControllerA

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *jumpButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 80, 100, 100)];
    jumpButton.backgroundColor = [UIColor redColor];
    jumpButton.layer.cornerRadius = 50;
    [jumpButton addTarget:self action:@selector(jumpButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [jumpButton setTitle:@"ToB" forState:UIControlStateNormal];
    [self.view addSubview:jumpButton];
    
    _testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 40, 40)];
    _testLabel.center = self.view.center;
    _testLabel.textAlignment = NSTextAlignmentCenter;
    _testLabel.backgroundColor = [UIColor lightGrayColor];
    _testLabel.textColor = [UIColor redColor];
    [self.view addSubview:_testLabel];
}

- (void)jumpButtonClickAction {
    
    //TODO: 声明weakSelf 在Block中使用 方式Block循环引用
    __weak typeof(self) weakSelf = self;
    _controllerB = [[ViewControllerB alloc] init];
    
    //TODO: 控制器中点击测试按钮 通过Block的回调实现
    _controllerB.change_controllerA_labelTitleBlock = ^(NSString *title) {
        weakSelf.testLabel.text = [NSString stringWithFormat:@"点击了%@",title];
    };
    
    [self.navigationController pushViewController:_controllerB animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

B.h

#import <UIKit/UIKit.h>

@interface ViewControllerB : UIViewController

//TODO: 声明用来回调的 Block
@property (nonatomic, copy) void(^change_controllerA_labelTitleBlock)(NSString *title);

@end



B.m

#import "ViewControllerB.h"

@interface ViewControllerB ()

@end

@implementation ViewControllerB

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *buttonLeft = [[UIButton alloc] initWithFrame:CGRectMake(40, 100, 100, 100)];
    buttonLeft.backgroundColor = [UIColor blackColor];
    [buttonLeft setTitle:@"buttonLeft" forState:UIControlStateNormal];
    [buttonLeft addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonLeft];
    
    UIButton *buttonRight = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(buttonLeft.frame) + 40, 100, 100, 100)];
    buttonRight.backgroundColor = [UIColor blackColor];
    [buttonRight setTitle:@"buttonRight" forState:UIControlStateNormal];
    [buttonRight addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonRight];
    
}

- (void)buttonClickAction:(UIButton *)sender {
    
    //TODO: 判断 self.change_controllerA_labelTitleBlock 是否为空(必写)
    if (self.change_controllerA_labelTitleBlock) {
        
        //TODO: Block 会调给控制器A 值
        self.change_controllerA_labelTitleBlock(sender.titleLabel.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

//TODO: Block Set方法(必写)
- (void)setChange_controllerA_labelTitleBlock:(void (^)(NSString *))change_controllerA_labelTitleBlock {
    _change_controllerA_labelTitleBlock = change_controllerA_labelTitleBlock;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end


2.delegate


A.h

#import <UIKit/UIKit.h>
#import "ViewControllerB.h"

@interface ViewController : UIViewController <ViewControllerBDelegate>


@end


A.m


#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation ViewController

//跳转按钮事件
- (IBAction)buttonAction:(UIButton *)sender {

    ViewControllerB *vcB = [[ViewControllerB alloc] init];

    //设置vcB的代理
    vcB.delegate = self;

    //跳转到vcB
    [self.navigationController pushViewController:vcB animated:YES];


}
//实现协议方法
- (void)sendValue:(NSString *)string {

    _label.text = string;

}

@end


B.h

#import <UIKit/UIKit.h>

@protocol ViewControllerBDelegate <NSObject>

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

@end


@interface ViewControllerB : UIViewController

// 委托代理,代理一般需使用弱引用(weak)
@property(nonatomic, weak) id<ViewControllerBDelegate>delegate;

@end


B.m


#import "ViewControllerB.h"

@interface ViewControllerB ()
@property (weak, nonatomic) IBOutlet UITextField *textField;

@end

@implementation ViewControllerB

//back按钮点击事件
- (IBAction)buttonAction:(UIButton *)sender {

    //调用代理方法
    [_delegate sendValue:_textField.text];
    //跳转回vcA
    [self.navigationController popToRootViewControllerAnimated:YES];

}


@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值