问题:
当App中有多个耦合性不高的页面,而页面之间又牵扯到数据的一致性,此时就会发现什么代理,通知操作起来都会相当的复杂,单利也不错,但是单利这玩意生命周期。。。咳咳
举例:A页面 B页面 C页面....... 中都有数据需要一致性,其中一个页面中用户对其数据发生了改变,其他页面都需要相应的处理,这种情况在App非常常见:
直接上代码:
这里的解决办法是建立一个对数据专门处理的类,每个页面对数据进行改变后通过通知的方式来通知该类,然后该类对数据进行管理,同时其他 控制器页面对所处理的数据进行监听,这样就解决了控制器之间复杂的数据传输问题:
A页面|B页面|C页面
---- | ----|-----||
数据监听|数据改变通知 监听|数据改变通知 监听
统一由YYGDataSoource来负责接收 通知,收到通知后对数据进行操作|
-----------|-------------
A页面 直接 .h文件
#import "ViewController.h"
#import "YYGDataSoource.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *Alabel;
@property(strong,nonatomic) YYGDataSoource * dataSource;
@end
@implementation ViewController
-(void)awakeFromNib
{
self.dataSource=[YYGDataSoource new];
//设置观察者
[self.dataSource addObserver:self
forKeyPath:@"data"
options:NSKeyValueObservingOptionNew
context:NULL];
}
-(void)dealloc
{
[self.dataSource removeObserver:self forKeyPath:@"data"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self updataLabel];
}
-(void)updataLabel
{
self.Alabel.text=[self.dataSource.data stringValue];
}
#pragma mark Class Notification
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if (object == self.dataSource && [keyPath isEqualToString:@"data"])
{
[self updataLabel];
}
}
B页面
#import "BViewController.h"
#import "YYGDataLayer.h"
#import "YYGDataSoource.h"
#import "YYGNotification.h"
@interface BViewController ()
@property (weak, nonatomic) IBOutlet UILabel *Blabel;
@property(strong,nonatomic) YYGDataSoource * dataSource;
@end
@implementation BViewController
-(void)awakeFromNib
{
self.dataSource=[YYGDataSoource new];
[self.dataSource addObserver:self
forKeyPath:@"data"
options:NSKeyValueObservingOptionNew
context:NULL];
}
-(void)dealloc
{
[self.dataSource removeObserver:self forKeyPath:@"data"];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self updata];
NSLog(@"--%@--",self.Blabel.text);
}
- (IBAction)Bchange:(id)sender
{
NSLog(@"this is B");
int value=arc4random()%100;
[YYGDataLayer setData:value];
[[NSNotificationCenter defaultCenter]postNotificationName:YYGDataChangedNotification
object:nil
userInfo:nil];
}
-(void)updata
{
self.Blabel.text=[self.dataSource.data stringValue];
}
#pragma mark Class Notification
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if (object == self.dataSource && [keyPath isEqualToString:@"data"])
{
[self updata];
}
}
C页面
#import "CViewController.h"
#import "YYGDataSoource.h"
#import "YYGDataLayer.h"
#import "YYGNotification.h"
@interface CViewController ()
@property (weak, nonatomic) IBOutlet UILabel *Clabel;
@property(strong,nonatomic) YYGDataSoource * dataSource;
@end
@implementation CViewController
-(void)awakeFromNib
{
self.dataSource=[YYGDataSoource new];
[self.dataSource addObserver:self forKeyPath:@"data" options:NSKeyValueObservingOptionNew context:NULL];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self updata];
}
- (IBAction)Change:(UIButton *)sender {
NSLog(@"this is C");
int value=arc4random()%100;
[YYGDataLayer setOtherData:value];
[[NSNotificationCenter defaultCenter]postNotificationName:YYGDataChangedNotification object:nil userInfo:nil];
}
-(void)updata
{
self.Clabel.text=[self.dataSource.data stringValue];
}
#pragma mark Class Notification
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if (object == self.dataSource && [keyPath isEqualToString:@"data"])
{
[self updata];
}
}
@end
对数据处理的类
YYGDataLayer.m文件
#import <Foundation/Foundation.h>
@class YYGDataModel;
@interface YYGDataLayer : NSObject
+(YYGDataModel *)selectData;
+(void)setData:(int)Data;
+(YYGDataModel *)selectOtherData;
+(void)setOtherData:(int)Data;
@end
.h文件
#import "YYGDataLayer.h"
#import "YYGDataModel.h"
static int s_data = 55;
static int s_otherData = 15;
@implementation YYGDataLayer
+(YYGDataModel *)selectData
{
YYGDataModel * model=[YYGDataModel new];
model.data=@(s_data);
return model;
}
+(void)setData:(int)Data
{
if (Data!=s_data)
{
s_data=Data;
}
}
+(YYGDataModel *)selectOtherData
{
YYGDataModel * model=[YYGDataModel new];
model.data=@(s_otherData);
return model;
}
+(void)setOtherData:(int)Data
{
if (Data!=s_otherData)
{
s_otherData=Data;
}
}
@end
模型类 1
#import <Foundation/Foundation.h>
@interface YYGDataModel : NSObject
@property(strong,nonatomic) NSNumber * data;
@end
#import "YYGDataModel.h"
@implementation YYGDataModel
@end
重要!!!对数据统一处理类
#import <Foundation/Foundation.h>
@class YYGDataModel;
@interface YYGDataSoource : NSObject
@property(strong,nonatomic,readonly) NSNumber * data;
@end
#import "YYGDataSoource.h"
#import "YYGDataModel.h"
#import "YYGDataLayer.h"
#import "YYGNotification.h"
@interface YYGDataSoource()
@property(strong,nonatomic) NSNumber * data;
@end
@implementation YYGDataSoource
-(instancetype)init
{
if (self=[super init])
{
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(NotificationHandler)
name:YYGDataChangedNotification
object:nil];
[self updata];
}
return self;
}
-(void)updata
{
YYGDataModel * model1=[YYGDataLayer selectData];
YYGDataModel * model2=[YYGDataLayer selectOtherData];
self.data=@([model1.data intValue]+[model2.data intValue]);
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark Classs Notification
-(void)NotificationHandler
{
[self updata];
}
@end
QQ交流群 298975638
本文 demo