iOS 关于页面回调传值的总结(delegete、block)
2017-08-12 12:42
316 查看
应用方式为:
控制器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
控制器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
相关文章推荐
- IOS关于block回调的用法
- ios 关于使用异步网络请求时block回调的内存注意
- IOS中关于Block的用法总结
- iOS开发总结之block回调以AFN为基础封装的网络请求操作类
- iOS页面间传值的五种方式总结(Delegate/NSNotification/Block/NSUserDefault/单例)
- ios中关于block使用的精华总结
- iOS开发关于block学习的总结
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS 四种回调方法总结
- 关于ASP.NET页面打印技术的总结 【转】
- IOS自己封装的异步下载(get)Block方式回调给大家分享下
- iOS 关于友盟微信分享成功回调不走的问题
- 关于 iOS 批量打包的总结
- 关于C++函数指针、函数回调的相关总结(重载与多态)
- iOS非常重要的 block回调
- iOS-页面传值总结
- iOS 页面传值方式 单例,block,代理协议方法,通知中心
- 转载一篇关于ios block内存管理的文章
- ios关于手机拨打电话,回调问题