標籤:一對一 通用 設定代理 elf com mat object atomic 多個
一、代理
1.代理的介紹
代理是一種通用的設計模式
代理使用方式:A 讓 B 做件事,空口無憑,簽個協議。
所以代理有三部分組成:
委託方: 定義協議
協議 : 用來規定代理方可以做什麼,必須做什麼
代理方: 按照協議完成委託方的需求
2. 協議的介紹
協議是定義了一套公用的介面,是方法的列表,但是無法實現。
可以通過代理,實現協議中的方法。
協議是公用方法,一般寫在一個類裡面。
如果多個類都使用這個協議,可以寫成一個peotocol檔案。
3.代理的使用
(1)委託某人做某事
先建立一個Custom類,Custom類要買東西,就定下一個協議,實現協議的人,就實現custom協議裡的方法。
Custom.h
#import <Foundation/Foundation.h>@class Customer;@protocol CustomerDelegate <NSObject>@required-(void)custom:(Customer*)Customer buyThingNum:(int)count;@end@interface Customer : NSObject@property(nonatomic,weak)id<CustomerDelegate>delegate;-(void)buyThingNum:(int)count;@end
Custom.m
#import "Customer.h"@implementation Customer-(void)buyThingNum:(int)count{ if (self.delegate && [self.delegate respondsToSelector:@selector(custom:buyThingNum:)]) { [self.delegate custom:self buyThingNum:5]; }}@end
viewController.m
#import "ViewController.h"#import "Customer.h"@interface ViewController ()<CustomerDelegate>@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; Customer *custom = [[Customer alloc]init]; custom.delegate = self; [custom buyThingNum:5];}-(void)custom:(Customer*)Customer buyThingNum:(int)count{ NSLog(@"%d",count);}@end
(2)反向傳值
定義一個CustomViewController的類,在CustomViewController.h中定義協議。
#import <UIKit/UIKit.h>@protocol PassValueDelegate <NSObject>@required-(void)buyThingNum:(int)num;@end@interface CustomViewController : UIViewController@property(nonatomic,weak) id<PassValueDelegate>delegate;@end
在CustomViewController.m中定義一個方法,如果設定了代理,並且代理實現了協議的方法,那麼就執行代理方法。
#import "CustomViewController.h"@interface CustomViewController ()@end@implementation CustomViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor orangeColor];}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ if (self.delegate && [self.delegate respondsToSelector:@selector(buyThingNum:)]) { [self.delegate buyThingNum:5]; }}@end
在ViewController類中,設定代理,實現代理方法。
import "ViewController.h"#import "CustomViewController.h"@interface ViewController ()<PassValueDelegate>@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowColor];}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ CustomViewController *customVC = [[CustomViewController alloc]init]; customVC.delegate = self; [self presentViewController:customVC animated:YES completion:nil];}-(void)buyThingNum:(int)num{ NSLog(@"%d",num);}@end
二. 通知
1. 通知的發送: 系統通過一個叫通知中樞進行的,通知中樞是一個單例。
NSNotification *notifacation = [NSNotification notificationWithName:@"passValue" object:@"哈哈" userInfo:@{@"num":@"10"}]; [[NSNotificationCenter defaultCenter]postNotification:notifacation];
2.接收通知
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowColor]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(buyThingNum:) name:@"passValue" object:nil];}-(void)buyThingNum:(NSNotification *)noti{ NSString *wordStr = [noti object]; NSDictionary *dic = [noti userInfo];
//拿到通知的值
NSLog(@"%@",wordStr); NSLog(@"%@",dic);
}
3.移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"passVaule" object:self];
列印結果:
2017-06-24 21:53:01.021 DelegateTest[15607:2194987] 哈哈
2017-06-24 21:53:01.021 DelegateTest[15607:2194987] {
num = 10;
}
三、通知與代理的優缺點
(1)通知可以一對多通訊,代理只能一對一。
(2)代理的執行效率比較高。
(3)通知的使用比較簡單。
(4)通知太多的情況下,代碼比較難維護。
iOS---代理與協議以及通知的使用