2011-06-27 16 views
6

Tengo un comportamiento extraño con un objeto NSNotification.
Mi aplicación tiene un controlador de navegación, la primera vista es una vista de tabla y la segunda vista es solo un controlador de vista que muestra datos de la celda seleccionada.
Entonces, en este controlador de vista de datos, envío una notificación cuando presiono un botón. La notificación también funciona al principio.NSNotificación lleva al error de segmentación

PERO cuando vuelvo a la vista de tabla y presiono nuevamente el controlador de visualización de datos en la pila y toco el botón con la notificación, toda la aplicación falla sin registro de errores.
Xcode sólo pone de relieve esta línea:

[[NSNotificationCenter defaultCenter] 
postNotificationName:@"toggleNoteView" object:nil]; 

La función de dónde enviar la notificación:

- (IBAction) toggleNoteView: (id) sender 
{ 
    [[NSNotificationCenter defaultCenter] 
    postNotificationName:@"toggleNoteView" object:nil]; 
} 

Este es el receptor:

- (id)init { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(toggleNoteView:) 
              name:@"toggleNoteView" object:nil]; 
    ... 
} 

- (void) toggleNoteView:(NSNotification *)notif { 

    takingNotes = !takingNotes; 
} 

Editar: Ahora me hizo llegar algún error registros

2011-06-27 23:05:05.957 L3T[3228:707] -[UINavigationItemView toggleNoteView:]: unrecognized selector sent to instance 0x4b235f0 
2011-06-27 23:05:06.075 L3T[3228:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationItemView toggleNoteView:]: unrecognized selector sent to instance 0x4b235f0' 
*** Call stack at first throw: 
(
0 CoreFoundation      0x3634f64f __exceptionPreprocess + 114 
1 libobjc.A.dylib      0x370a2c5d objc_exception_throw + 24 
2 CoreFoundation      0x363531bf -[NSObject(NSObject) doesNotRecognizeSelector:] + 102 
3 CoreFoundation      0x36352649 ___forwarding___ + 508 
4 CoreFoundation      0x362c9180 _CF_forwarding_prep_0 + 48 
5 Foundation       0x35c45183 _nsnote_callback + 142 
6 CoreFoundation      0x3631e20f __CFXNotificationPost_old + 402 
7 CoreFoundation      0x362b8eeb _CFXNotificationPostNotification + 118 
8 Foundation       0x35c425d3 -[NSNotificationCenter postNotificationName:object:userInfo:] + 70 
9 Foundation       0x35c441c1 -[NSNotificationCenter postNotificationName:object:] + 24 
10 L3T         0x0003d17f -[Container toggleNoteView:] + 338 
11 CoreFoundation      0x362bf571 -[NSObject(NSObject) performSelector:withObject:withObject:] + 24 

Respuesta

8

No olvide quitar el observador cuando descarga una vista. Básicamente, lo que sucede es que cuando publicas una notificación en una vista que no existe, no puede ejecutar el selector, lo que bloquea tu aplicación.

-(void)dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
    [super dealloc]; 
} 
3

Desde su descripción, parece que cada vez que se coloca un controlador de vista, va a crear una nueva instancia del controlador de vista de hacer eso.

Si ese es el caso, primero debe asegurarse de no estar goteando ese controlador de vista cuando regrese a la vista de tabla.

Luego, en el método dealloc de ese objeto, anule su suscripción de las notificaciones.

-(void)dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
    //other deallocation code 
    [super dealloc]; 
} 
Cuestiones relacionadas