代理是iOS开发中经常会碰到的开发设计模式之一,主要是进行一些参数的传递,由代理对象、委托者、协议三部分组成。如果想灵活在两个控制器之间进行逆向传值,或者在自定义的View中传递一些特有的参数,则必须清楚代理对象、委托者、协议三部分之间的关联
一、基础知识:
代理是一种通用的设计模式,既然可以被大多数人所用,必定有一个代码的规范来约束,进而驾驭它,利用它达到各种目的,OC中使用@Protocol实现协议,定义方法规范来约束代理双方,代理则负责实现定义的方法,委托对象则负责检测代理对象是否将设定的协议方法实现
-->作用:
1).用来声明一个或多个方法 (不能声明属性,也不能实现方法,只能用来写方法的声明)
2).只要某个类遵守了这个协议.就相当于拥有这个协议中的所有的方法声明.而不用自己去重新定义
-->主要思路:
1).定义1个协议,里面声明代理类需要实现的方法列表
2).创建1个代理类:遵守上面的代理协议并实现方法
3).在需要代理的类中,定义1个对象属性类型为id且遵守代理协议的属性
4).在代理的类中,调用代理对象的方法
1、@Protocol 协议
协议是公共的定义,如果只是某个类使用,我们常做的就是写在某个类中。如果是多个类都是用同一个协议,建议创建一个Protocol文件,在这个文件中定义协议
2、通过代码看委托和代理的不同之处:FirstViewController:代理,SecondViewController:委托
(1) 代理对象的实现文件 --->FirstViewController.m文件
#import "FirstViewController.h"
#import "SecondViewController.h"
//添加可约束双方的代理协议
@interface FirstViewController ()<SecondViewControllerDelegate>
@property (nonatomic, strong) UILabel *label;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"第一个控制器";
self.view.backgroundColor = [UIColor lightGrayColor];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 150, 150)];
label.text = @"==我是一个好人==";
[label setTextColor:[UIColor redColor]];
_label = label;
[self.view addSubview:label];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 400, 150, 150)];
[btn setTitle:@"跳转下个控制器" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)click:(UIButton *)btn
{
// 很重要的一步:不能忘记设置代理为本身,这是因为代理对象通过id类型指针对代理对象进行了弱引用,去实现消息的传递
SecondViewController *vc = [[SecondViewController alloc]init];
vc.delegate = self;
[self.navigationController pushViewController:vc animated:YES];
}
-(void)secondViewController:(NSString *)content{
//主线程刷新界面 --刷新UI在主线程
// _label.text = content;
// dispatch_async(dispatch_get_main_queue(), ^{
// _label.text = content;
// });
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
_label.text = content;
}];
}
@end
(2) 委托 -----> SecondViewController.h文件:定义协议和方法
#import <UIKit/UIKit.h>
@class SecondViewController;
//定义的协议
@protocol SecondViewControllerDelegate <NSObject>
//定义一个方法 可提供多个参数,参数为你想传递的东西,可设置不同类型,如果是自定义的View,第一个参数必须是本身,仿照UITableView那样,这样方便以后在同一个控制器使用的时候进行tag区分
//方法可以用@optional 、@required进行区分必须实现还是可选择的去实现
-(void)secondViewController:(NSString *)content;
@end
@interface SecondViewController : UIViewController
//定义一个weak类型的全局代理属性
@property (nonatomic, weak) id<SecondViewControllerDelegate> delegate;
@end
SecondViewController.m文件
#import "SecondViewController.h"
@interface SecondViewController ()
@property (nonatomic, strong) UITextField *textField;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"第二个控制器";
self.view.backgroundColor = [UIColor grayColor];
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 300, 200, 30)];
_textField = textField;
textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:textField];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 400, 150, 150)];
[btn setTitle:@"返回上个控制器" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
//退出键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[_textField resignFirstResponder];
}
-(void)back:(UIButton *)btn{
// 监视方法是否被执行,如果没有,会发生崩溃
// 调用代理对象进行传值
if ([self.delegate respondsToSelector:@selector(secondViewController:)]) {
[self.delegate secondViewController:_textField.text];
}
[self.navigationController popViewControllerAnimated:YES];
}
@end
二、代理模式的优势
delegate更加灵活,传递多个参数也很方便,也使得它运载的东西更多,因为用weak属性声明,使用起来效率和性能更好,也会自动释放,比较安全可靠