2011-08-23 14 views
6

Nella mia applicazione ci sono due viewControllers come FirstViewController e DetailViewController. Quando si tocca una cella di tabella, si sposta su DetailViewController. In DetailViewController, voglio modificare e ricaricare la vista tabella FirstViewControllerCome utilizzare NSNotification

Come posso utilizzare NSNotification per questo problema?

Ecco il metodo che voglio implementare NSNotification roba

-(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

Può essere utile a voi http://mac-objective-c.blogspot.com/2009/02/nsnotifications-broadcasting-mechanism.html – Tendulkar

+0

Questo non è un compito per una notifica. Dai un'occhiata a TableViewProgrammingGuide. È possibile iniziare qui: http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/TableView/Introduction/Introduction.html HTH –

+0

Suggerirei, è preferibile utilizzare il modello delegato piuttosto che la notifica. –

risposta

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

dopo la notifica da detailViewController e aggiungere firstViewController come l'osservatore.

Assicurarsi di rimuovere fireViewController dall'elenco degli osservatori da viewDidUnload.

In questo momento si sta aggiungendo detailViewController come osservatore.

Problemi correlati