如何在目标中使用NSNotification c
问题描述:
我不知道如何在我们的iphone应用程序中使用NSNotification。以及委托和NSNotification之间的区别,因为两者都是通过对象进行通信。如何在目标中使用NSNotification c
并给出实例。
答
=> NSNotificationCenter提供了一个集中毂,通过该应用程序的任何部分可以通知和从应用程序的任何其他部分变更通知。
=>观察员向通知中心注册,以通过指定操作对特定事件做出响应。
=>每次发生事件时,通知都会通过它的调度表,并向该事件的任何注册观察者发送消息。
在Objective C使用NS-通知
//从那里你要传递的数据
[[NSNotificationCenter defaultCenter]postNotificationName:@"TeamTable" object:hdImage userInfo:nil];
在这里写下
** TeamTable是通知观察者名称(唯一名称)
** hdImage是你想要传递给另一个控制器的数据
现在写在控制器这些代码要接受 数据
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(detailsData:) name:@"TeamTable" object:nil];
}
-(void)detailsData:(NSNotification*)sender{
//In sender it contain All received data
}
为他们释放前的物体取出观察员,以防止被发送更多的消息是很重要的。
-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"TeamTable" object:nil];
}
有关NS-通知的更多详细信息,您可以点击此链接http://nshipster.com/nsnotification-and-nsnotificationcenter/