用作UINavigationBar titleVIew时未调用UISearchBar委托?

问题描述:

我有一个UITableViewController,我已经指定为UISearchBarDelegate。直到现在,我已经以编程方式将UISearchBar添加到表的headerView中,并且没有任何问题。用作UINavigationBar titleVIew时未调用UISearchBar委托?

我开始跑出来的屏幕房地产,所以我决定杀了我正常的UINavigationController冠军(这是文本),并添加如下代码,从表中移动我的搜索栏到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]; 

此代码按预期工作 - 我的UINavigationBar现在是UISearchBar。 但是,我的代表方法:

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

...不再被调用。我突出了,我已经确认UISearchBar的委托确实是自我的,视图控制器。奇怪的是,这个委托的方法仍然是所谓就好:

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

任何想法,为什么UINavigationBar的正在吞噬我的委托电话?我错过了什么?

我想你写错了方法签名。它应该是: - searchBarTextDidBeginEditing: 以下是用于文本编辑的所有UISearchBarDelegate方法。

– searchBar:textDidChange: 

– searchBar:shouldChangeTextInRange:replacementText: 

– searchBarShouldBeginEditing: 

– searchBarTextDidBeginEditing: 

– searchBarShouldEndEditing: 

– searchBarTextDidEndEditing: 

UISearchBarDelegate

+0

谢谢你,我需要第二双眼睛看到。应该再次检查UISearchBarDelegate协议!这解决了它。 – makdad 2010-06-13 09:08:39

+0

这很正常。 XCode很难支持自动完成和检查。我也在委托和继承方面犯了同样的错误。我经常做viewWillAppear {}而不是viewWillAppear:(BOOL)动画 – vodkhang 2010-06-13 12:51:00