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

2017-08-12

应用方式为:

控制器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





注明:本文章属于转载,仅供行业人员学习交流使用,文章版权属于原创作者,在此向原创者致敬,感谢原创作者为大家学习交流提供精品内容。

站方声明:IThao123是为广大互联网从业者免费提供学习交流的平台,如果侵犯了原创著作权,请联系站方删除,给你带来不便,深表歉意。

顶部