您的位置:首页 > 其它

oc 中协议 protocol 的使用

2018-03-02 11:10 435 查看
1、把相同的属性和方法抽取出来:一、构建父类;二、构建协议;

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
都可以使用,使用方法和上面��例子相同

总结

协议作为数据源或代理使用的步骤如下:

一、声明协议

二、设置协议的遵守者

三、实现协议方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: