1 、Protocol介绍
Protocol为进行网络中的数据交换而建立的规则、标准或约定。用于不同系统中实体间的通信。两个实体要想通信,必须有“同一种语言”,而且,对于通信内容,怎样通信和何时通信,都必须遵守一定的规定,这些规定就是协议。亦可简单地定义为:控制两实体间数据交换的一套规则。
对于iOS中的Protocol,大约可以理解为A让B做一件事情,做好了告诉A。
代码使用,稍微贴下代码,具体一步一步请自行去学习。本文不做解释。
1、首先创建一个Delegate
#import <Foundation/Foundation.h>
@protocol WKProtocolDelegate <NSObject>
@required
- (void)startRun;
@optional
- (void)startThinking;
@end
2、在VC中遵守协议 全程代码如下,
#import "ViewController.h"
#import "WKProtocolDelegate.h"
#import "ViewController2.h"
@interface ViewController ()<WKProtocolDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100, 100);
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)click:(id)sender{
ViewController2 *vc = [[ViewController2 alloc] init];
vc.delegate =