1.代理包含三部分 1.委托对象2.代理对象3.协议(代理主要用于传值,或者传递方法)
2.协议名称的定义
(1)名称 定义一般是 类名+Delegate
@protocol FirstDelegate <NSObject>
-(void)getVale:(NSString *)nameStr;
@end
(2)声明一个代理属性(用weak 防止循环引用)
@property(weak,nonatomic)id<FirstDelegate> delegate;
(3)设置代理对象
FirstViewController *controller = [FirstViewController new];
controller.delegate =self;
(4)代理对象遵守协议实现协议里面的方法
@interface ViewController ()<FirstDelegate>
-(void)getVale:(NSString *)nameStr
{
NSLog(@"代理方法走了================%@====================",nameStr);
}
(5)协议调用
if ([_delegate respondsToSelector:@selector(getVale:)]) {
[_delegate getVale:self.textfield.text];
[self.navigationController popViewControllerAnimated:YES];
}
网友评论