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

时间:2010-06-13 03:15:37

标签: iphone uinavigationbar uisearchbar

我有一个UITableViewController,我已指定为UISearchBarDelegate。到目前为止,我已经以编程方式将UISearchBar添加到表的headerView中,并且没有任何问题。

我开始耗尽屏幕空间,所以我决定杀死我的普通UINavigationController标题(这是文本),并添加了以下代码,将我的SearchBar从表移动到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吞下我的委托电话?我错过了什么?

1 个答案:

答案 0 :(得分:7)

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

– searchBar:textDidChange:

– searchBar:shouldChangeTextInRange:replacementText:

– searchBarShouldBeginEditing:

– searchBarTextDidBeginEditing:

– searchBarShouldEndEditing:

– searchBarTextDidEndEditing:

UISearchBarDelegate