一、首先介紹Protocol
1稼跳、協(xié)議protocol說明
protocol:定義公用的一套接口酪术,但不提供具體的實(shí)現(xiàn)方法。
協(xié)議約定可選擇實(shí)現(xiàn)的方法和必須實(shí)現(xiàn)的方法经瓷。
@required:必須實(shí)現(xiàn)的方法
@optional:可選是否實(shí)現(xiàn)的方法
2爆哑、協(xié)議protocol的使用
//創(chuàng)建協(xié)議文件protocolDelegate
//1.new file -->Objective-C File-->選擇Protocol,創(chuàng)建Protocol文件
//protocol文件只有.h文件舆吮,只聲明方法揭朝。
#import <Foundation/Foundation.h>
@protocol protocolDelegate <NSObject>
@required
//必須實(shí)現(xiàn)的方法
- (void)eat;
- (void)drink;
// 可選實(shí)現(xiàn)的方法
@optional
- (void)readBook;
- (void)writeCode;
@end
//創(chuàng)建student類和programmer類具體實(shí)現(xiàn)對應(yīng)方法
//2.創(chuàng)建student類
//Student.h
#import <Foundation/Foundation.h>
#import "protocolDelegate.h"
@interface Student : NSObject <protocolDelegate>
@end
//Student.m
#import "Student.h"
@implementation Student
- (void)eat {
NSLog(@"%@",@"我是學(xué)生队贱,我必須得吃飯,我吃米飯");
}
- (void)drink {
NSLog(@"%@",@"我是學(xué)生潭袱,我必須得喝水柱嫌,我喝飲料");
}
- (void)readBook {
NSLog(@"%@",@"我是學(xué)生,我會讀書");
}
@end
//3.創(chuàng)建Programma類
//Programma.h
#import <Foundation/Foundation.h>
#import "protocolDelegate.h"
@interface Programmer : NSObject <protocolDelegate>
@end
//Programma.m
#import "Programmer.h"
@implementation Programmer
- (void)eat {
NSLog(@"%@",@"我是工程師屯换,我必須得吃飯编丘,我吃火鍋");
}
- (void)drink {
NSLog(@"%@",@"我是工程師,我必須得喝水趟径,我喝啤酒");
}
- (void)writeCode {
NSLog(@"%@",@"我是工程師瘪吏,我會寫代碼");
}
@end
//調(diào)用方法
//在viewcontroller里調(diào)用
#import "ViewController.h"
#import "Student.h"
#import "Programmer.h"
@interface ViewController ()
@property (nonatomic, strong) Student *student;
@property (nonatomic, strong) Programmer *programmer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self dinerTogether];
}
// 聚餐
- (void)dinerTogether {
[self eatTogether];
[self drinkTogether];
[self doMySelf];
}
- (void)eatTogether {
if ([self.student respondsToSelector:@selector(eat)]) {
[self.student eat];
}
if ([self.programmer respondsToSelector:@selector(eat)]) {
[self.programmer eat];
}
}
- (void)drinkTogether {
if ([self.student respondsToSelector:@selector(drink)]) {
[self.student drink];
}
if ([self.programmer respondsToSelector:@selector(drink)]) {
[self.programmer drink];
}
}
- (void)doMySelf {
if ([self.student respondsToSelector:@selector(readBook)]) {
[self.student readBook];
}
if ([self.programmer respondsToSelector:@selector(writeCode)]) {
[self.programmer writeCode];
}
}
#pragma mark - getter and setter
- (Student *)student {
if (!_student) {
_student = [[Student alloc] init];
}
return _student;
}
- (Programmer *)programmer {
if (!_programmer) {
_programmer = [[Programmer alloc] init];
}
return _programmer;
}
@end
注:一般多個類要實(shí)現(xiàn)共同的方法,又各自有自己要實(shí)現(xiàn)的方法時蜗巧,可使用Protocol
一掌眠、delegate簡介
代理設(shè)計模式,是iOS中一種消息傳遞的方式幕屹,由代理對象蓝丙、委托者、協(xié)議組成望拖。
- 協(xié)議:用來指定代理可以做什么渺尘,必須做什么。
- 代理:根據(jù)指定協(xié)議说敏,完成委托方需要實(shí)現(xiàn)的方法鸥跟。
- 委托:根據(jù)指定協(xié)議,指定代理必須完成和可以完成方法盔沫。
二医咨、delegate的使用
1、傳值
//下一控制器傳值給上一控制器
//下一控制器NextViewController文件
//NextViewController.h文件
#import <UIKit/UIKit.h>
@protocol NextViewControllerDelegate <NSObject>
- (void)sendValue:(NSString *)string;
@end
@interface NextViewController : UIViewController
@property (nonatomic, weak) id<NextViewControllerDelegate> delegate;
@end
//NextViewController.m文件
#import "NextViewController.h"
@interface NextViewController ()
//創(chuàng)建一個TextField輸入要傳端值
@property (nonatomic, strong) UITextField *tf_sendValue;
//創(chuàng)建返回上一控制器的按鈕
@property (nonatomic, strong) UIButton *btn_backVc;
@end
@implementation NextViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 添加TextField
UITextField *tf_sendValue = [[UITextField alloc]init];
tf_sendValue.frame = CGRectMake(0, 100, self.view.frame.size.width, 30);
tf_sendValue.textAlignment = 1;
tf_sendValue.placeholder = @"請輸入要傳遞的值";
self.tf_sendValue = tf_sendValue;
[self.view addSubview:tf_sendValue];
// 添加返回上一控制器的按鈕
UIButton *btn_backVc = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 35)];
[btn_backVc setTitle:@"返回上一控制器" forState:UIControlStateNormal];
[btn_backVc setBackgroundColor:[UIColor redColor]];
btn_backVc.center = self.view.center;
self.btn_backVc = btn_backVc;
[self.view addSubview:btn_backVc];
//添加點(diǎn)擊事件
[self.btn_backVc addTarget:self action:@selector(clickBackBtn) forControlEvents:UIControlEventTouchUpInside];
}
- (void)clickBackBtn{
[self.navigationController popViewControllerAnimated:YES];
NSString *str = self.tf_sendValue.text;
if (str.length>0) {
if ([self.delegate respondsToSelector:@selector(sendValue:)]) {
[self.delegate sendValue:str];
}
}
}
@end
//上一控制器遵循協(xié)議架诞,實(shí)現(xiàn)代理方法拟淮,獲取傳遞的值
//ViewController.m文件
#import "ViewController.h"
#import "NextViewController.h"
@interface ViewController ()<NextViewControllerDelegate>
//創(chuàng)建一個Label顯示下一界面?zhèn)鬟^來的值
@property (nonatomic, strong) UILabel *lb_showStr;
//創(chuàng)建push下一界面按鈕
@property (nonatomic, strong) UIButton *btn_nextVc;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"第一個控制器";
self.view.backgroundColor = [UIColor whiteColor];
// 添加lb_showStr
UILabel *lb_showStr = [[UILabel alloc]init];
lb_showStr.frame = CGRectMake(0, 100, self.view.frame.size.width, 30);
lb_showStr.textAlignment = NSTextAlignmentCenter;
lb_showStr.text = @"顯示下一界面的傳值";
self.lb_showStr = lb_showStr;
[self.view addSubview:lb_showStr];
// 添加push下一控制器按鈕
UIButton *btn_nextVc = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 35)];
[btn_nextVc setTitle:@"push下一界面" forState:UIControlStateNormal];
[btn_nextVc setBackgroundColor:[UIColor redColor]];
btn_nextVc.center = self.view.center;
self.btn_nextVc = btn_nextVc;
[self.view addSubview:btn_nextVc];
//添加點(diǎn)擊事件
[self.btn_nextVc addTarget:self action:@selector(clickPushBtn) forControlEvents:UIControlEventTouchUpInside];
}
- (void)clickPushBtn{
NextViewController *nextVc = [[NextViewController alloc]init];
[self.navigationController pushViewController:nextVc animated:YES];
nextVc.delegate = self;
}
- (void)sendValue:(NSString *)string{
self.lb_showStr.text = string;
}
@end
2、傳遞事件
//點(diǎn)擊自定義view谴忧,傳遞view點(diǎn)擊事件
//1.MyView.h
#import <UIKit/UIKit.h>
@protocol MyViewDelegate <NSObject>
- (void)myViewDelegateFunc;
@end
@interface MyView : UIView
//聲明代理屬性
@property (nonatomic, weak) id<MyViewDelegate> myViewDelegate;
@end
//2.MyView.m
#import "MyView.h"
@implementation MyView
//點(diǎn)擊view很泊,響應(yīng)代理方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
if ([_myViewDelegate respondsToSelector:@selector(myViewDelegateFunc)]) {
[_myViewDelegate myViewDelegateFunc];
}
}
@end
//實(shí)現(xiàn)代理方法
#import "ViewController.h"
#import "MyView.h"
@interface ViewController ()<MyViewDelegate>
@property (nonatomic, strong) MyView *myView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MyView *myView = [[MyView alloc]init];
myView.myViewDelegate = self;
self.myView = myView;
myView.backgroundColor = [UIColor redColor];
myView.frame = CGRectMake(100, 100, 100, 100);
myView.center = self.view.center;
myView.userInteractionEnabled = YES;
[self.view addSubview:self.myView];
}
#pragma mark - myViewDelegate代理方法
- (void)myViewDelegateFunc{
NSLog(@"myViewDelegate的代理 點(diǎn)擊view");
}
@end
注:代理實(shí)現(xiàn)了不同視圖之間的數(shù)據(jù)交互,只有某一事件觸發(fā)才會被調(diào)用沾谓。在使用代理時委造,代理者要遵循代理,設(shè)置代理均驶,實(shí)現(xiàn)代理方法争涌。只有設(shè)置了代理,才能調(diào)用代理方法辣恋。