Objective-C 协议(protocol)
来源:互联网 发布:淘宝店设计装修多少钱 编辑:程序博客网 时间:2024/05/20 18:46
今天我们来学习Objectivc-C中的协议。学习主要分三部分:
- 协议是什么?
- 协议、委托和代理是什么关系?
- 协议的作用是什么,可以用它实现什么,使用场景是什么?
- 如何使用协议?(Demo)
一、协议是什么?
协议(protocol)是Objective-C中一个非常重要的语言特性,从概念上讲,非常类似于JAVA中接口。一个协议其实就是一系列有关联的方法的集合,如最常见的tableViewDelegate和tableViewDataSorce。协议中的方法并不是由协议本身去实现,而是由遵循这个协议的其他类来实现。换句话说,协议只是完成对协议函数的声明而并不管这些协议函数的具体实现。
二、协议、委托和代理是什么关系?
协议是Objective-C里的一种特性(语法层面),委托也可以叫做代理,是一种设计模式(架构设计层面)。在iOS开发中,通过协议来实现代理模式。
三、协议的使用场景?
最初自己定义协议是用来传值,而且是从后向前传值。A页面进入B页面,在B页面输入文本,返回到A页面显示。当然协议不仅仅是用来传值,如果当前类不能完成,需要别的类替他来完成某个操作时,这时就可以使用协议。比如:我们自定义了一个视图(V),现在想通过点击视图上的button进入下一级页面,但是该视图不在当前viewController(C)上,不能直接push到下一级页面,这时我们就可以为V制定协议,让C遵循该协议并设置C为V的delegate,让C实现V声明的方法,这样就可以实现我们想要的功能。(PS.例子不太好,想到好的例子会及时更新,此外多参考系统的SDK)
四、如何使用协议?
本文就用传值来作为demo。首先介绍传值思路,协议传值6步:(从后向前传)
第二页
1.创建协议
2.创建代理人
3.在需要的地方让代理人执行协议方法
第一页
4.签订协议
5.设定自己为第二页代理人
6.实现协议方法
上代码:
SecondViewController.h文件:
#import<UIKit/UIKit.h>
//第一步:制订协议(协议名称:类名+Delegate)
@protocol SecondViewControllerDelegate <NSObject>
- (void)passValue:(NSString *)value;
@end
@interface SecondViewController :UIViewController
@property (weak,nonatomic)IBOutletUITextField *sencondTextField;
//第二步:添加代理属性delegate
// 注意:1.这里用assign而不用retain是为了防止引起循环引用;
// 注意:1.这里用assign而不用retain是为了防止引起循环引用;
// 2.属性类型是id(任意)类型,另外约定作为代理的对象必须实现SecondViewControllerDelegate协议;
// 3.delegate前不加*
@property (nonatomic,assign)id<SecondViewControllerDelegate> delegate;
@end
SecondViewController.m文件
#import"SecondViewController.h"
@interfaceSecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[superviewDidLoad];
[superviewDidLoad];
}
//第五步:开始传值,使用代理对象调用代理方法
- (IBAction)backClick:(id)sender {
[self.delegatepassValue:self.sencondTextField.text];
[self.navigationControllerpopViewControllerAnimated:YES];
}
- (IBAction)backClick:(id)sender {
[self.delegatepassValue:self.sencondTextField.text];
[self.navigationControllerpopViewControllerAnimated:YES];
}
@end
MainViewController.m文件:
#import"MainViewController.h"
#import"SecondViewController.h"
//第三步:遵循协议
@interfaceMainViewController ()<SecondViewControllerDelegate>
@end
@implementation MainViewController
- (void)viewDidLoad {
[superviewDidLoad];
[superviewDidLoad];
}
//第四步:设置代理人
- (IBAction)enterClick:(id)sender
{
SecondViewController *secondVC = [[SecondViewControlleralloc]init];
secondVC.delegate =self;
[self.navigationControllerpushViewController:secondVCanimated:YES];
- (IBAction)enterClick:(id)sender
{
SecondViewController *secondVC = [[SecondViewControlleralloc]init];
secondVC.delegate =self;
[self.navigationControllerpushViewController:secondVCanimated:YES];
}
//第六步:实现协议方法(接收传值)
- (void)passValue:(NSString *)value
{
self.mainLabel.text = value;
}
- (void)passValue:(NSString *)value
{
self.mainLabel.text = value;
}
@end
运行程序时可以通过设置断点来查看代码的执行顺序,当在第二个页面点击返回按钮,程序会执行backClick:方法,然后是[self.delegatepassValue:self.sencondTextField.text];诶,注意了,见证奇迹的时刻到了!接下来程序跳到了MainViewController中执行passValue:方法,然后回到SecondViewConroller执行[self.navigationControllerpopViewControllerAnimated:YES];返回上移页面,就这样我们完成了传值操作。
现在我们回过头想一想,协议到底帮我们做了什么事,为什么要使用它,如果不用它,怎么实现从后向前传值。
参考链接:http://blog.csdn.net/lovefqing/article/details/8268397
0 0
- Objective-C 协议(protocol)
- Objective-C 协议(protocol)
- Objective-C 协议(protocol)
- Objective-C 协议(protocol)
- Objective-C 协议(protocol)
- Objective-C 协议(protocol)
- Objective-c 协议(protocol)
- Objective C 协议(Protocol)
- Objective-C 协议 protocol
- objective c学习:协议(protocol)
- Objective-C 协议(protocol)
- objective-c---ARC、block、协议(protocol)
- Objective-C中的协议Protocol
- Objective-C之protocol协议
- Objective-c的协议protocol
- Objective-C 13 协议protocol
- Objective-C category (类别) 和 protocol (协议)
- objective-c中的正式协议 protocol(十一)
- 技术管理者培训小结五: 研发团队常见问题
- 第一次使用git,小计一下步骤
- 像素格式的定义
- mysql语句:批量更新多条记录的不同值
- tomcat+apache集群配置
- Objective-C 协议(protocol)
- web.xml配置项学习笔记
- 判断一个字符串能否通过添加一个字符变成回文串
- Hibernate(七)——继承映射
- 关于Android Studio 出现Unrecognized Windows Sockets error: 10107: Socket creation failed解决办法
- Android Studio中的git配置中文件修改颜色
- 第三周项目3-求集合并集
- 第四周-项目2 - 建设“单链表”算法库
- xml文件的处理