2011-06-18 1 views
1

J'essaie de faire apparaître une alerte indiquant "Aucun enregistrement trouvé" si l'utilisateur effectue une recherche et que le prédicat renvoie zéro.Essayer de faire apparaître un UIAlert si mon prédicat de recherche renvoie zéro

J'ai essayé, mais l'alerte n'est jamais appelée. Je viens d'avoir une tableview vide.

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 

    if (self.sBar.text != nil){ 

     NSPredicate *template = [NSPredicate predicateWithFormat:@"name contains[cd] $SEARCH OR optionOne contains[cd] $SEARCH OR optionTwo contains[cd] $SEARCH"]; 
     NSDictionary *replace = [NSDictionary dictionaryWithObject:self.sBar.text forKey:@"SEARCH"]; 
     NSPredicate *predicate = [template predicateWithSubstitutionVariables:replace]; 

     [fetchedResultsController.fetchRequest setPredicate:predicate]; 

    } 

    [[fetchedResultsController fetchedObjects] count]; 

    if (fetchedResultsController.fetchedObjects != nil) { 

     //do nothing 

    } else { 

     //display alert 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"No records match." 
                  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 

    } 

    NSError *error = nil; 
    if (![[self fetchedResultsController] performFetch:&error]) { 

     // Handle error 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); // Fail 
    }   

    [self.myTable reloadData]; 

    [sBar resignFirstResponder]; 

} 
+0

mis à jour avec le nouveau code – RyeMAC3

Répondre

0

Ha ha! Got it ...

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 

    if (self.sBar.text != nil) { 

     NSPredicate *template = [NSPredicate predicateWithFormat:@"name contains[cd] $SEARCH OR optionOne contains[cd] $SEARCH OR optionTwo contains[cd] $SEARCH"]; 
     NSDictionary *replace = [NSDictionary dictionaryWithObject:self.sBar.text forKey:@"SEARCH"]; 
     NSPredicate *predicate = [template predicateWithSubstitutionVariables:replace]; 

     [fetchedResultsController.fetchRequest setPredicate:predicate]; 

    } 

    NSError *error = nil; 
    if (![[self fetchedResultsController] performFetch:&error]) { 

     // Handle error 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     exit(-1); // Fail 
    } 

    if ([[fetchedResultsController fetchedObjects] count] != 0) { 

     [self.myTable reloadData]; 
     [sBar resignFirstResponder]; 

    } 
    else { 

     //display alert 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"No records match." 
                  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 

    } 
} 
0

Un prédicat n'a pas de -count. Un prédicat is an expression that evaluates to true or false. C'est tout.

Vous pouvez demander au NSFetchedResultsController pour son -fetchedObjects (qui retourne un tableau), puis demander à ce tableau pour son -count, mais vous ne pouvez le faire après que vous invoquez -performFetch:.

+0

... ajouté nombre de fetchedObjects, mais toujours rien. – RyeMAC3

+0

@ RyeMAC3 avez-vous fait 'performFetch:' en premier? –

Questions connexes