代码之家  ›  专栏  ›  技术社区  ›  makdad

当用作UINavigationBar标题视图时未调用UISearchBar委托?

  •  5
  • makdad  · 技术社区  · 14 年前

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

    我开始用尽屏幕空间,所以我决定取消我的普通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的委托确实是self,即视图控制器。奇怪的是,这个委托方法的调用仍然很好:

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

    你知道为什么UINavigationBar会拒绝我的代表电话吗??我错过了什么?

    1 回复  |  直到 14 年前
        1
  •  7
  •   vodkhang    14 年前

    我认为你写错了方法签名。应该是: searchBarTextDidBeginEditing:

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

    UISearchBarDelegate