現在發通知的viewController中
NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:tf.text,@"textField",nil];
[[NSNotificationCente rdefaultCenter] postNotificationName:@"name" object:self userInfo:dict];
然后在要顯示的viewController中
注冊通知:
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(done:) name:@"name" object:nil];
添加觀察者之后,一定要在dealloc里面記得移除。
顯示:
NSDictionary* dict =[notification userInfo];
label.text= [dict objectForKey:@"textField"];
通知 NSNotificationCenter
發送通知:調用觀察者處的方法。
[[NSNotificationCenter?defaultCenter]?postNotificationName:@"mytest" object:searchFriendArray];
參數:
postNotificationName:通知的名字,也是通知的唯一標示,編譯器就通過這個找到通知的。
object:傳遞的參數
注冊通知:即要在什么地方接受消息
[[NSNotificationCenter?defaultCenter]??addObserver:self?selector:@selector(mytest:)?name:@" mytest"?object:nil];
- (void) mytest:(NSNotification*) notification
{
id obj = [notification object];//獲取到傳遞的對象
//NSDictionary* dict =[notification userInfo];
//label.text= [dict objectForKey:@"textField"];
}
參數介紹:
addObserver: 觀察者,即在什么地方接收通知;
selector: 收到通知后調用何種方法;
name: 通知的名字,也是通知的唯一標示,編譯器就通過這個找到通知的。