模式分为3大类
1.创建型模式
工厂模式
工厂方法模式
抽象工厂模式
创建者模式
原型模式
单例模式
2.结构型模式
外观模式
适配器模式
代理模式
装饰模式
桥模式
组合模式
享元模式
3.行为模式
模板方法模式
管缠着模式
状态模式
策略模式
职责链模式
命令模式
访问者模式
调停者模式
备忘录模式
迭代器模式
解析器模式
委托模式是结构体模式的一种,主要功能是像block一样有一个回调传值的功能
下面是代码 建立一个视图控制器,2个VIEW
第一个VIEW有一个BUTTON 还有一个LABEL button用来推送下一个视图
第二个VIEW有有一个BUTTON 还有一个TEXTFILED button用来返回上一个视图
效果:通过点击第二个view 在text输入值 返回然后在laberl上看到txt的值
#import <UIKit/UIKit.h>
#import "KViewController.h"
@interface ViewController : UIViewController<Protocol>
@end
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)nextView:(id)sender {
UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
KViewController *KV=[story instantiateViewControllerWithIdentifier:@"KViewController"];
KV.argeement=self;
[self.navigationController pushViewController:KV animated:YES];
}
-(void)callback:(NSString *)string{
self.myLabel.text=string;
}
@end
第二个VIEW
#import <UIKit/UIKit.h>
@protocol Protocol <NSObject>
-(void)callback:(NSString*)string;
@end
@interface KViewController : UIViewController
@property id<Protocol> argeement;
@end
#import "KViewController.h"
@interface KViewController ()
@property (weak, nonatomic) IBOutlet UITextField *myText;
@end
@implementation KViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)goback:(id)sender {
[self.navigationController popViewControllerAnimated:nil];
if (self.argeement != nil && [self.argeement conformsToProtocol:@protocol(Protocol)]) {
[self.argeement callback:self.myText.text];
}
}
总结:委托模式主要是实现了一个回调传值的作用