码迷,mamicode.com
首页 > 其他好文 > 详细

OC通知实例

时间:2015-11-10 09:21:52      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

#import "NotificationClass.h"

#import "AnotherNotificationClass.h"

 

@implementation NotificationClass

 

//注册通知

-(void)addNotification

{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFun:) name:@"change" object:nil];

}

 

-(void)changeFun:(NSNotification *)sender

{

    NSDictionary *getDic=[sender userInfo];

    NSLog(@"NotificationClass 接收到通知事件,传递的值:%@",getDic);

}

//发送通知

-(void)postNotification

{

    NSDictionary *dictionary=[NSDictionary dictionaryWithObject:@"newValue" forKey:@"obj"];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:self userInfo:dictionary];//发送给自己

    

    if (!another) {

        another=[[AnotherNotificationClass alloc]init];

    }

    [[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:another userInfo:dictionary];//发送给another

 

    

}

 

//移除通知

-(void)removeNotification

{

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}

 

 

@end

 

OC通知实例

标签:

原文地址:http://www.cnblogs.com/shuxiachahu123/p/4951851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!