2010-06-13 7 views
5

J'ai un UITableViewController que j'ai spécifié comme UISearchBarDelegate. Jusqu'à présent, j'avais ajouté par programme l'UISearchBar à headerView de la table, et il n'y avait pas de problèmes.Le délégué UISearchBar n'est pas appelé lorsqu'il est utilisé comme UINavigationBar titleVIew?

Je commence à manquer d'écran de l'immobilier, alors j'ai décidé de tuer mon titre normal UINavigationController (ce qui était le texte), et ajouté le code suivant, déplacer ma SearchBar de la table à la UINavigationBar:

// (Called in viewDidLoad) 
// Programmatically make UISearchBar 
UISearchBar *tmpSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,45)]; 
tmpSearchBar.delegate = self; 
tmpSearchBar.showsCancelButton = YES; 
tmpSearchBar.autocorrectionType = UITextAutocorrectionTypeNo; 
tmpSearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone; 
[self set_searchBar:tmpSearchBar]; 
[tmpSearchBar release]; 
self.navigationItem.titleView = [self _searchBar]; 

Ce code fonctionne comme prévu - mon UINavigationBar est maintenant un UISearchBar. Cependant, ma méthode déléguée:

/** Only show the cancel button when the keyboard is displayed */ 
- (void) searchBarDidBeginEditing:(UISearchBar*) lclSearchBar 
{ 
    lclSearchBar.showsCancelButton = YES; 
} 

... n'est plus appelé. J'ai cassé, et j'ai confirmé que le délégué de UISearchBar est en effet self, le contrôleur de vue. Bizarrement, cette méthode de délégué est encore appelé très bien:

/** Run the search and resign the keyboard */ 
- (void) searchBarSearchButtonClicked:(UISearchBar *)lclSearchBar 
{ 
    _deepSearchRan = NO; 
    [self runSearchForString:[[self _searchBar] text] isSlowSearch:NO]; 
    [lclSearchBar resignFirstResponder]; 
} 

Toutes les idées pourquoi UINavigationBar avale mes appels délégués ?? Qu'est-ce que je rate?

Répondre

7

Je pense que vous écrivez une mauvaise signature de méthode. Il devrait être: - searchBarTextDidBeginEditing: Voici toutes les méthodes UISearchBarDelegate pour l'édition de texte.

– searchBar:textDidChange: 

– searchBar:shouldChangeTextInRange:replacementText: 

– searchBarShouldBeginEditing: 

– searchBarTextDidBeginEditing: 

– searchBarShouldEndEditing: 

– searchBarTextDidEndEditing: 

UISearchBarDelegate

+0

Merci, je avais besoin d'un deuxième ensemble d'yeux pour voir. Vous auriez dû vérifier le protocole UISearchBarDelegate une fois de plus! Cela l'a réparé. – makdad

+0

C'est normal. XCode supporte mal l'auto-complétion et la vérification. J'ai aussi fait la même erreur avec le délégué et l'héritage. Je fais souvent viewWillAppear {} au lieu de viewWillAppear: (BOOL) animé – vodkhang

Questions connexes