目錄
1.1 單例模式
1.2 KVO
1.3 KVC
1.4 通知
1.1 單例模式
+ (JDSingleton *)sharedInstance;
+ (JDSingleton *)sharedInstance{
static JDSingleton *single = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
single = [[JDSingleton alloc]init];
});
return single;
}
1.2 KVO
//KVO概述:
KVO,即:Key-Value Observing,它提供一種機(jī)制,當(dāng)指定的對象的屬性被修改后,則對象就會接受到通知。
//KVO的優(yōu)點(diǎn):
當(dāng)有屬性改變,KVO會提供自動的消息通知。這樣開發(fā)人員不需要自己去實(shí)現(xiàn)這樣的方案,每次屬性改變了就發(fā)送消息通知。 這是KVO機(jī)制提供的最大的優(yōu)點(diǎn)。開發(fā)人員不需要添加任何代碼,不需要設(shè)計自己的觀察者模型,直接可以在工程里使用。 其次,KVO的架構(gòu)非常的強(qiáng)大,可以很容易的支持多個觀察者觀察同 一個屬性,以及相關(guān)的值。
//使用步驟如下:
1. 注冊,指定被觀察者的屬性,
2. 實(shí)現(xiàn)回調(diào)方法
3. 觸發(fā)回調(diào)方法
4. 移除觀察
1.注冊個觀察者的類
#import Foundation/Foundation.h(尖括號)
@interface Music : NSObject
{
NSString *musicName;
}
@end
2.實(shí)例化這個類
@property (nonatomic ,strong)Music *music;
3.注冊觀察者
[self.music setValue:textField.text forKey:@"musicName"];
4.移除觀察者
[self.music removeObserver:self forKeyPath:@"musicName"];
5.實(shí)現(xiàn)觀察方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
// UILabel *label = (UILabel *)[self.view viewWithTag:100];
// 如果改變的屬性是"musicName"
if ([keyPath isEqualToString:@"musicName"]) {
// 將 當(dāng)前的musicName屬性的值 賦值給UILabel
// label.text = [self.music valueForKey:@"musicName"];
// 輸出改變前的值
NSLog(@"old musicName is %@",[change objectForKey:@"old"]);
// 輸出改變后的值
NSLog(@"new musicName is %@",[change objectForKey:@"new"]);
}
}
示例:http://www.itdecent.cn/p/7ff617320155
1.3 KVC
.h
#import < Foundation/Foundation.h >
@interface MapModel : NSObject
@property (nonatomic, copy) NSString *result;
@property (nonatomic, copy) NSString *message;
@end
.m
#import "MapModel.h"
@implementation MapModel
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
NSLog(@"缺少%@", key);
}
@end
使用
MapModel *mapModel = [[MapModel alloc] init];
[mapModel setValuesForKeysWithDictionary:dic];
1.4 通知
#pragma mark 1.在需要的時候發(fā)送通知
//添加字典,將label的值通過key值設(shè)置傳遞
NSDictionary *dict =[[NSDictionary alloc] initWithObjectsAndKeys:self.textFieldOne.text,@"textOne",self.textFieldTwo.text,@"textTwo", nil];
//創(chuàng)建通知
NSNotification *notification = [NSNotification notificationWithName:@"tongzhi" object:nil userInfo:dict];
//通過通知中心發(fā)送通知
[[NSNotificationCenter defaultCenter] postNotification:notification];
//接收頁面注冊通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:@"tongzhi" object:nil];
//3.實(shí)現(xiàn)方法
- (void)tongzhi:(NSNotification *)text{
NSLog(@"%@",text.userInfo[@"textOne"]);
NSLog(@"-----接收到通知------");
}
//4.移除通知
移除通知:removeObserver:和removeObserver:name:object:
[[NSNotificationCenter defaultCenter] removeObserver:observer name:nil object:self];
注意參數(shù)notificationObserver為要刪除的觀察者,一定不能置為nil。
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。