代理:
创建代理方:先要在.h文件中创建协议,设置代理方法(weak),参数类型即为想要传递的数据类型;设置满足代理的属性;
然后在.m文件方法中判断代理对象是否能够响应,能响应就让响应对象执行代理方法.
执行代理方:先要遵守代理协议,设置自身控制器为代理(易忽略),然后实现代理方法实现数据回传.
block:
创建block方:先在.h文件中设置block属性方法(copy),参数类型即为想要传递的数据类型.然后在.m文件中判断能否响应,能响应就执行block方法
执行block方:在创建了一个创建block方类的对象后,该对象便可实现block方法,在方法体中实现数据回传.
通知:
通知方:设置通知事件名称和字典(字典内即为要传递的数据)
接收方:接收对应名称的通知事件,执行收到通知后应实现的方法(取出字典中的数据即是要回传的数据),接受完后删除通知
.h文件
#import <UIKit/UIKit.h>
@protocol MydeleBlockNotyViewDelegate <NSObject>
// 代理可选方法
@optional
- (void)str:(NSString *)string;
@end
@interface MydeleBlockNotyView : UIView
// 遵守协议的代理属性
@property (nonatomic,strong) id<MydeleBlockNotyViewDelegate> delegate;
// Block方式
@property (nonatomic,copy) void (^myblock)(NSSting *);
@end
.m文件
#import "MydeleBlockNotyView.h"
@implementation MydeleBlockNotyView
- (void)drawRect:(CGRect)rect {
// 代理的按钮
UIButton *redBtn = [[UIButton alloc] initWithFrame:CGRectMake(50,50,80,80)];
redBtn.backgroundColor = [UIColor redColor];
[redBtn setTitle:@"代理" forState:(UIControlStateNormal)];
[redBtn addTarget:self action:@selector(dele) forControlEventTouchs:(UIControlEventTouchUpInside)];
[self addSubview:redBtn];
// Block的按钮
UIButton *blueBtn = [[UIButton alloc] initWithFrame:CGRectMake(150,50,80,80)];
blueBtn.backgroundColor = [UIColor blueBtn];
[blueBtn setTitle:@"Block" forState:(UIControlStateNormal)];
[blueBtn addTarget:self action:@selector(block) forControlEventTouchs:(UIControlEventTouchUpInside)];
[self addSubview:blueBtn];
// 通知的按钮
UIButton *greenBtn = [[UIButton alloc] initWithFrame:CGRectMake(250,50,80,80)];
greenBtn.backgroundColor = [UIColor greenBtn];
[greenBtn setTitle:@"通知" forState:(UIControlStateNormal)];
[greenBtn addTarget:self action:@selector(noty) forControlEventTouchs:(UIControlEventTouchUpInside)];
[self addSubview:greenBtn];
}
// 代理的点击事件
- (void)dele {
NSString *myText = @"这是代理回传的值";
if ([self.delegate respondsToSelector:@selector(str:)]) {
[self.delegate str:myText];
}
}
// Block的点击事件
- (void)dele {
NSString *myText = @"这是Block回传的值";
if (self.myblock) {
self.myblock(myText);
}
}
// 通知的点击事件
- (void)dele {
NSString *myText = @"这是通知回传的值";
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNoty" object:nil userInfo:@{@"myText", myText}];
}
@end
回传接收数据方
#import "ViewController.h"
#import "MydeleBlockNotyView.h"
@interface ViewController ()<MydeleBlockNotyViewDelegate>
@property (weak,nonatomic) IBOutlet UILabel *TextLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MydeleBlockNotyView *myView = [[MydeleBlockNotyView alloc] init];
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
myView.frame = CGRectMake(0,100,screenW,200);
myView.backgroundColor = [UIColor orangeColor];
myView.delegate = self;
[self.view addSubview:myView];
// Block实现
myView.myblock = ^(NSSting *str){
self.myLabel.text = str;
};
// 注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(not:) name:@"myNoty" object:nil];
}
// 实现代理方法
- (void)str:(NSString *)string {
self.myLabel.text = string;
}
// 实现通知方法
- (void)not:(NSNotification *)noty {
self.myLabel.text = noty.userInfo[@"myText"];
}
// 执行完后移除通知
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
执行效果图:
初学OC时,对三种回传方式的总结,代理和Block用法相似,传值方和接收方需有一定的关联,而通知不需要,例如当有三个视图时,要把C视图的数据传给A,这时用代理和Block比较麻烦,可以用通知来传递,而一般不太复杂的建议用代理或者Block