oc 中协议 protocol 的使用
2018-03-02 11:10
435 查看
1、把相同的属性和方法抽取出来:一、构建父类;二、构建协议;
2、类的属性和方法都是和这个类关联的;协议的属性和方法不和任何类进行关联,是独立的;
3、协议包含方法(类方法、对象方法)和属性;正是因为这个特性,所以协议可以作为
4、协议作为
5、协议有
6、检测协议中
7、协议的继承
8、遵守协议
在
一开始加载完成之后输出内容为:
点击
声明协议数据源
实现数据源方法
最后,当调用方法
2、作为
- 声明协议代理
实现代理方法
最后,当点击
一、声明协议
二、设置协议的遵守者
三、实现协议方法
2、类的属性和方法都是和这个类关联的;协议的属性和方法不和任何类进行关联,是独立的;
3、协议包含方法(类方法、对象方法)和属性;正是因为这个特性,所以协议可以作为
数据源;根据协议传入的数据不同显示不同的页面,如
UITableView的数据源;
@property (nonatomic, weak, nullable) id <UITableViewDataSource> dataSource; @property (nonatomic, weak, nullable) id <UITableViewDelegate> delegate;
4、协议作为
属性一般使用
weak修饰,避免循环引用
@property (nonatomic, weak, nullable) id <UITableViewDataSource> dataSource; @property (nonatomic, weak, nullable) id <UITableViewDelegate> delegate;
5、协议有
required和
optional两种修饰,默认是
required
required表示是必须实现的
optional表示是可以实现,也可以不实现
@protocol UITableViewDataSource<NSObject> @required - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; @optional - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // 其他的... @end
6、检测协议中
optional的方法是否被实现了
respondsToSelector
NSString *thisSegmentTitle; if ([self.dataSource respondsToSelector:@selector(titleForSegmentAtIndex:)]) { thisSegmentTitle = [self.dataSource titleForSegmentAtIndex:index]; }
7、协议的继承
@protocol MyProtocol<NSObject> @end
8、遵守协议
@interface MyClass : NSObject <MyProtocol> ... @end
使用
协议可以用来view显示的数据源,也可以用来
view的代理使用,典型的例子就是
UITableView,下面是自己模拟协议的使用; (有
返回值的作为
数据源使用,
没有返回值的作为
代理使用)
MyView.h文件内容如下
#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @protocol MyViewDataSource <NSObject> @optional - (NSUInteger)numbersOfPeople; - (NSString *)titleForView; @end @protocol MyViewDelegate <NSObject> @optional - (void)myViewPayButtonDidClick; @end @interface MyView : UIView @property (nonatomic, weak, nullable) id<MyViewDataSource> dataSource; @property (nonatomic, weak, nullable) id<MyViewDelegate> delegate; - (void)reload; @end NS_ASSUME_NONNULL_END
MyView.m文件内容如下
#import "MyView.h" @implementation MyView - (void)reload { NSUInteger numbers = 0; if (self.dataSource && [self.dataSource respondsToSelector:@selector(numbersOfPeople)]) { numbers = [self.dataSource numbersOfPeople]; } NSLog(@"人数为 : %ld", numbers); NSString *title = nil; if (self.dataSource && [self.dataSource respondsToSelector:@selector(titleForView)]) { title = [self.dataSource titleForView]; } NSLog(@"标题 : %@", title); } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { if (self.delegate && [self.delegate respondsToSelector:@selector(myViewPayButtonDidClick)]) { [self.delegate myViewPayButtonDidClick]; } } @end
在
ViewController.m文件中使用到类
MyView
#import "ViewController.h" #import "MyView.h" @interface ViewController ()<MyViewDataSource, MyViewDelegate> @end @implementation ViewController #pragma mark - DataSource - (NSUInteger)numbersOfPeople { return 100; } - (NSString *)titleForView { return @"title"; } #pragma mark - Delegate - (void)myViewPayButtonDidClick { NSLog(@"购买---"); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. MyView *mView = [[MyView alloc] init]; mView.backgroundColor = [UIColor redColor]; mView.frame = CGRectMake(0, 100, 100, 100); mView.dataSource = self; mView.delegate = self; [self.view addSubview:mView]; [mView reload]; } @end
一开始加载完成之后输出内容为:
人数为 : 100 标题 : title
点击
view的时候输出内容为:
购买---
分析:
1、作为view显示的数据源
声明协议数据源
@protocol MyViewDataSource <NSObject> @optional - (NSUInteger)numbersOfPeople; - (NSString *)titleForView; @end
view需要遵守协议,需要设置数据源
@interface ViewController ()<MyViewDataSource, MyViewDelegate> @end // --- mView.dataSource = self;
实现数据源方法
#pragma mark - DataSource - (NSUInteger)numbersOfPeople { return 100; } - (NSString *)titleForView { return @"title"; }
最后,当调用方法
[mView reload];就可以把数据源从
ViewController传递给
MyView,这个就是数据源的传递过程
2、作为
view的代理
- 声明协议代理
@protocol MyViewDelegate <NSObject> @optional - (void)myViewPayButtonDidClick; @end
view需要遵守协议,需要设置代理
@interface ViewController ()<MyViewDataSource, MyViewDelegate> @end // --- mView.delegate = self;
实现代理方法
#pragma mark - Delegate - (void)myViewPayButtonDidClick { NSLog(@"购买---"); }
最后,当点击
view的时候,就会把调用代理方法
myViewPayButtonDidClick,(ps:可以通过参数传递数据)
ps:上面的例子是使用
view作为例子,事实上,任何对象
NSObject都可以使用,使用方法和上面��例子相同
总结
协议作为数据源或代理使用的步骤如下:一、声明协议
二、设置协议的遵守者
三、实现协议方法
相关文章推荐
- OC--———协议protocol的基本使用
- oc 中协议 protocol 的使用
- 黑马程序员——OC学习之协议protocol的使用
- oc基础-protocol协议的使用
- OC中的类别Category-协议Protocol-…
- OC-ARC机制、数据类型block、 protocol协议、Foudation框架
- 协议protocol和代理delegate的使用
- 【Objective-C】OC中自定义对象的归档基本概念和使用方法(实现NSCoding协议)
- thrift使用传输协议TCompactProtocol
- OC Protocol----协议
- OC协议protocol详解
- OC学习--代理与协议的使用
- 【Objective-C】OC中自定义对象的归档基本概念和使用方法(实现NSCoding协议)
- OC中的类别Category-协议Protocol-扩展Extension
- iOS开发之OC篇(13)—— Protocol(协议)、Delegate(代理)
- OC中的协议(@protocol)
- 黑马程序员——OC中的协议:protocol
- 【IOS 开发学习总结-OC-20】★★★objective-c面向对象——协议(protocol)
- OC学习:protocol的使用
- 黑马程序员_OC基础13_协议protocol