【问题标题】:UISearchBar delegate not called when used as UINavigationBar titleVIew?用作 UINavigationBar titleVIew 时未调用 UISearchBar 委托?
【发布时间】:2010-06-13 03:15:36
【问题描述】:

我有一个指定为 UISearchBarDelegate 的 UITableViewController。到目前为止,我已经以编程方式将 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 会吞下我的委托调用吗?我错过了什么?

【问题讨论】:

    标签: iphone uinavigationbar uisearchbar


    【解决方案1】:

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

    – searchBar:textDidChange:
    
    – searchBar:shouldChangeTextInRange:replacementText:
    
    – searchBarShouldBeginEditing:
    
    – searchBarTextDidBeginEditing:
    
    – searchBarShouldEndEditing:
    
    – searchBarTextDidEndEditing:
    

    UISearchBarDelegate

    【讨论】:

    • 谢谢,我需要第二双眼睛才能看到。应该再检查一次 UISearchBarDelegate 协议!这解决了它。
    • 这很正常。 XCode 对自动完成和检查的支持很差。我在委托和继承方面也犯了同样的错误。我经常做 viewWillAppear{} 而不是 viewWillAppear:(BOOL)animated
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 2014-03-18
    相关资源
    最近更新 更多