---
title: "NSNotificationCenter 例子"
author: "修博龙泉"
date: "2012-10-26T13:56:37Z"
tags: []
categories: ["移动开发"]
slug: "nsnotificationcenter"
draft: false
lastmod: "2012-10-26T14:08:12Z"
id: 1705928
url: "https://longquan.iteye.com/blog/1705928"
---

.h

```java

 [[nsnotificationcenter defaultcenter] postNotificationName:@"wantuNewMessage"                                                    object:@"10"];

```


.m

```java
- (void)viewDidAppear:(BOOL)animated{
    [[nsnotificationcenter defaultcenter]
     addObserver:self
     selector:@selector(showNewMessage:)
     name:@"wantuNewMessage"
     object:nil];
}

- (void)viewDidDisappear:(BOOL)animated{
    [[nsnotificationcenter defaultcenter] removeObserver:self];
}
- (void)showNewMessage:(NSNotification*) notification{
    NSLog(@"wantu.showNewMessage");
    [notification object];
}

```