2011-08-23 39 views
6

在我的應用程序中有兩個viewControllers作爲FirstViewControllerDetailViewController。 當點擊一個表單元格時,它導航到DetailViewController。在DetailViewController中,我想編輯並重新加載FirstViewController的表格視圖如何使用NSNotification

如何使用NSNotification解決此問題?

這是我想要實現NSNotification東西

-(IBAction) save{ 
strSelectedText=theTextField.text; 

[NSNotificationCenter defaultCenter]; 
NSNotification* notification = [NSNotification notificationWithName:@"MyNotification" object:self]; 
[[NSNotificationCenter defaultCenter] postNotification:notification]; 

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



[self.navigationController popViewControllerAnimated:YES]; 
} 
+0

這可能是你http://mac-objective-c.blogspot.com/2009/02/nsnotifications-broadcasting-mechanism.html – Tendulkar

+0

這個有用的是不是一個通知的任務。只需查看TableViewProgrammingGuide。你可以從這裏開始:http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/TableView/Introduction/Introduction.html HTH –

+0

我建議,你最好使用委託模式而不是通知。 –

回答

9
-(void)viewDidLoad { 

[NSNotificationCenter defaultCenter]; 
NSNotification* notification = [NSNotification notificationWithName:@"MyNotification" object:self]; 
[[NSNotificationCenter defaultCenter] postNotification:notification]; 

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

} 


-(IBAction) save{ 

[[NSNotificationCenter defaultCenter] postNotificationName:MyNotification object:sender]; 

//this will go to where you implement your selector objFirstViewController. 

} 

-(void)objFirstViewController:(NSNotification *)notification { 

} 
0

後從detailViewController通知,並添加firstViewController作爲觀察者的方法。

確保從viewDidUnload的觀察者列表中刪除fireViewController。

現在你要添加detailViewController作爲觀察者。