Sono consapevole dei motivi standard, il motivo per cui non sarebbe stato ricevuto una notifica:NSNotificationCenter osservatore non vengono ricevute le notifiche
- deallocando o annullare l'oggetto registrato.
- Rimozione dell'oggetto come osservatore.
- Non si registra come un osservatore.
- Registrazione per la notifica errata o l'invio della notifica sbagliata.
Posso tranquillamente dire che sono dannatamente sicuro che nessuno di questi sta accadendo. Suppongo che molto probabilmente l'oggetto sia annullato e ricreato a un certo punto, ma si registra per la notifica al momento dell'inizializzazione.
Qui è dove mi registro:
/**
* initialises an object with a unique file url
*
* @param url the url to set as the file url
*/
- (id)initWithFileURL:(NSURL *)url
{
if (self = [super initWithFileURL:url])
{
self.entries = [[NSMutableArray alloc] init];
// we want to be notified when a note has changed
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(noteChanged)
name:@"com.andbeyond.jamesvalaitis.notesChanged"
object:self];
NSLog(@"Just registered for 'com.andbeyond.jamesvalaitis.notesChanged'");
}
return self;
}
Qui è dove inserisco la notifica:
/**
* save the note content
*/
- (void)saveNote
{
if (_isChanged)
{
// save to the text view to the note's contents
self.note.noteContent = self.noteView.text;
// post a notification that we changed the notes
[[NSNotificationCenter defaultCenter] postNotificationName:@"com.andbeyond.jamesvalaitis.notesChanged" object:nil];
NSLog(@"Just posted 'com.andbeyond.jamesvalaitis.notesChanged'");
// make sure we know it's already saved
_isChanged = NO;
}
}
Questo è il metodo che non viene chiamato:
/**
* called when a note has changed
*/
- (void)noteChanged:(NSNotification *)notification
{
NSLog(@"Just received for 'com.andbeyond.jamesvalaitis.notesChanged'");
// save the notes
[self saveToURL:self.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success)
{
if (success)
NSLog(@"Note updated.");
}];
}
Questo è la console che chiarisce che registro e postifico la notifica entrambi:
2012-11-15 13: 27: 50,958 iCloud personalizzato [11269: 907] appena registrata per 'com.andbeyond.jamesvalaitis.notesChanged'
2012-11-15 13: 28: 24,184 iCloud personalizzato [11269: 907] appena pubblicato 'com.andbeyond.jamesvalaitis.notesChanged' url
The whole project can be found here.
Aggiungi il metodo noteChanged al post ... – chrislhardin
il tuo selettore è ancora come questo @selector (noteChanged) .. allora come chiamerà questo - (void) noteChanged: notifica (NSNotification *) ??? devi cambiare il selettore in questo modo @selector (noteChanged :) –
@DineshRaja: Ciao Dinesh e James, mi trovo di fronte a un problema simile e ho ricevuto tutte le risposte su questo post, applicando i suggerimenti; ancora le cose non hanno funzionato per me. Sono in grado di inviare la notifica con successo e dichiarare anche l'Observer. Ma la logica in cui viene effettuato il controllo non viene eseguita. Come devo condividere il codice? O dovrei pubblicare una domanda piuttosto doppia? –