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

通知(NSNotificationCenter)

时间:2015-09-17 23:08:52      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:

通知

   如今天遇到了一个问题,长按相片,将此相片传到发布说说那个控制器中,不管用push,还是其它方法都无法实现,后来发现一个view添加到了Windows上面了,view添加完毕了就移除了和这个控制器一点关系都没有,这时一个大牛(朋友)用了"通知‘这个方法,解决了我的难题,记录下.

 1.发送通知

/**   postNotificationName: 是发布的通知的名字,谁要注册通知时,必须和这个名字一致

 *   Object :是要传递的对象或参数,如果不传为nil (id类型).

**/ 

[[NSNotificationCenter defaultCenter] postNotificationName:@"MJNOtificationPicLongPress"  Object:mj.image];

2. 注册通知

/**    addObserver: 是监听哪个类

 *   selector: 添加方法  name:通知的名字,必须上发布通知的名字一致

**/ object: 传过来的对象

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

3. 实现通知的方法

/**   

 *   NSNotification:通知的类型  [noti object] 是取出通知中传进来的参数或对象

**/

 -(void)picLongPress:(NSNotification*)noti{

      CirclePublishViewController *cpCtrl = [[CirclePublishViewController alloc] init];
      cpCtrl.image = [noti object];
     [self.navigationController pushViewController:cpCtrl animated:YES];
}

4.移除通知

- (void)dealloc{

[[NSNotificationCenter defaultCenter] removeObserver:self];


}

 

通知(NSNotificationCenter)

标签:

原文地址:http://www.cnblogs.com/TheYouth/p/4817870.html

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