我不知道如何在我們的iphone應用程序中使用NSNotification。以及委託和NSNotification之間的區別,因爲兩者都是通過對象進行通信。如何在目標中使用NSNotification c
並給出實例。
我不知道如何在我們的iphone應用程序中使用NSNotification。以及委託和NSNotification之間的區別,因爲兩者都是通過對象進行通信。如何在目標中使用NSNotification c
並給出實例。
許多樣品是可用 NSNotification userinfo example?
=> 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/