object-c之通知

  如果学生没有来上课,老师给家长发送通知

 1 #import "Parents.h"
 2 // #import "Teacher.h"
 3 @implementation Parents
 4 
 5 - (instancetype)init
 6 {
 7     self = [super init];
 8     if (self) {
 9         // 注册通知
10         // addObserver:观察者,即在什么地方接收通知,
11         // selsctor:受到通知后响应的方法,是一个选择器,用于指定一个接收指定通知的方法
12         // name:响应的名字,即通知的名字,只有与通知中心的通知名相匹配时,才可能接收该通知
13         // object:传递参数 谁去响应事件 object为nil时除了自己,所有的通知名为isAttenClass的都能接受到通知
14         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(attendClass:) name:@"isAttendClass" object:nil];
15     }
16     return self;
17 }
18 
19 - (void)attendClass:(NSNotification *)notification
20 {
21     //   Teacher *teacher = notification.object;
22     NSLog(@"触发通知");
23     NSLog(@"家长你好,%@",[notification userInfo]);
24 }
25 
26 
27 
28 - (void)dealloc
29 {
30     // 移除指定通知
31     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"isAttendClass" object:nil];
32     
33     // 移除所有通知
34     [[NSNotificationCenter defaultCenter] removeObserver:self];
35 }
36 
37 @end

 

 

 

 

 

 1 #import <Foundation/Foundation.h>
 2 
 3 #import "Parents.h"
 4 
 5 @interface Teacher : NSObject
 6 
 7 @property (nonatomic,assign) BOOL isAttendClass;
 8 
 9 - (void)postNotification;
10 
11 @end
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 #import "Teacher.h"
23 
24 @implementation Teacher
25 
26 
27 - (void)postNotification
28 {
29     Parents *parents = [[Parents alloc]init];
30     _isAttendClass = NO;
31     
32     // 发送通知
33     // object:发送通知给谁
34     if (!_isAttendClass) {
35         [[NSNotificationCenter defaultCenter]postNotificationName:@"isAttendClass" object:parents userInfo:@{@"通知":@"孩子没来上课"}];
36     }
37 }
38 
39 @end

 

posted @ 2015-07-20 19:29  凝月霜雪  阅读(476)  评论(0编辑  收藏  举报