2011-08-23 18 views
6

En mi aplicación hay dos viewControllers como FirstViewController y DetailViewController. Cuando toca en una celda de tabla, navega hasta DetailViewController. En DetailViewController, quiero editar y volver a cargar la vista de tabla FirstViewControllerCómo utilizar NSNotification

¿Cómo puedo utilizar NSNotification para este problema?

Aquí está el método que desea implementar NSNotification cosas

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

Puede ser útil para usted http://mac-objective-c.blogspot.com/2009/02/nsnotifications-broadcasting-mechanism.html – Tendulkar

+0

Esto no es una tarea para una notificación. Simplemente eche un vistazo a TableViewProgrammingGuide. Puede comenzar aquí: http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/TableView/Introduction/Introduction.html HTH –

+0

Sugiero que sea mejor que use un patrón de delegado en lugar de una notificación. –

Respuesta

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

posterior a la notificación de detailViewController y añadir FirstViewController como el observador.

Asegúrese de quitar fireViewController de la lista de observadores de viewDidUnload.

Ahora está agregando detailViewController como observador.