(1)首先要明白基本的注册和发送原则:
NSNotificationCenter是专门供程序中不同类间的消息通信而设置的,使用起来极为方便,
长话短说。
设置通知,就是说要在什么地方(哪个类)接受通知,一般在初始化中做。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveAeraMessage:) name:@"recieveAeraMessage" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveAeraMessage:) name:@"recieveAeraMessage" object:nil];
我仅对以上参数做以说明:addObserver 这个是观察者,就是说 在什么地方接收通知;
selector 这个是收到通知后,调用何种方法;
name: 这个是通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
发送通知,就是说此时要调用观察者处的方法。
[[NSNotificationCenter defaultCenter] postNotificationName:@"recieveAeraMessage" object:nil userInfo:dataDic];
[[NSNotificationCenter defaultCenter] postNotificationName:@"recieveAeraMessage" object:nil userInfo:dataDic];
我仅对以上参数做以说明:
postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
object:传递的参数
发送通知时,默认调用test方法。
//通知响应的方法
- (void)recieveAeraMessage:(NSNotification *)notification{
NSDictionary *dict = [notification userInfo];
NSNumber *Apresent = dict[@"Apresent"];
NSNumber *AdistanceHungred = dict[@"AdistanceHungred"];
NSNumber *AdistanceTen = dict[@"AdistanceTen"];
NSNumber *AdistanceOne = dict[@"AdistanceOne"];
NSNumber *Bpresent = dict[@"Bpresent"];
NSNumber *BdistanceHungred = dict[@"BdistanceHungred"];
NSNumber *BdistanceTen = dict[@"BdistanceTen"];
NSNumber *BdistanceOne = dict[@"BdistanceOne"];
dispatch_async(dispatch_get_main_queue(), ^{
int Adistance,Apre,Bdistance,Bpre;
Adistance = [AdistanceHungred intValue] * 100 + [AdistanceTen intValue] * 10 + [AdistanceOne intValue];
Apre = [Apresent intValue];
Bdistance = [BdistanceHungred intValue] * 100 + [BdistanceTen intValue] * 10 + [BdistanceOne intValue];
Bpre = [Bpresent intValue];
self.area2_perTF.text = [NSString stringWithFormat:@"%d",Apre];
self.area2_disTF.text = [NSString stringWithFormat:@"%d",Adistance];
self.area3_perTF.text = [NSString stringWithFormat:@"%d",Bpre];
self.area3_disTF.text = [NSString stringWithFormat:@"%d",Bdistance];
});
}