【问题标题】:searchdisplaycontroller: change the text of the searchbaruisearchdisplaycontroller:更改搜索栏的文本
【发布时间】:2010-03-04 10:12:30
【问题描述】:

问题的简短描述: 我想设置搜索栏的文本,而不自动触发绑定到它的搜索显示控制器。

问题的详细描述: 我有一个带有搜索栏和搜索显示控制器的 iphone 应用程序。 searchdisplay 控制器用于自动完成。对于自动完成,我使用 sqlite 数据库。用户输入关键字的前几个字母,结果显示在搜索显示控制器的表格中。为每个键入的字符执行一个 sql 选择查询。这部分工作正常,字母已输入,结果可见。

问题如下:如果用户从表中选择一行,我想将搜索栏的文本更改为在自动完成结果表中选择的文本。我还想隐藏搜索显示控制器。这是行不通的。搜索显示控制器消失后,搜索栏中的文本框为空。我不知道出了什么问题。我没想到像更改文本框的文本这么简单的事情会变得如此复杂。

我尝试用 2 种方法更改文本: 首先在 didSelectRowAtIndexPath 方法中(用于搜索显示控制器的搜索结果表),但这没有帮助。当搜索显示控制器处于活动状态(动画消失)时,文本就在那里,但之后文本框为空。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"didSelectRowAtIndexPath"); if (allKeywords.count > 0) { didSelectKeyword = TRUE; self.selectedKeyword = (NSString *)[allKeywords objectAtIndex: indexPath.row]; NSLog(@"keyword didselectrow at idxp: %@", self.selectedKeyword); shouldReloadResults = FALSE; [[self.mainViewController keywordSearchBar] setText: selectedKeyword]; //shouldReloadResults = TRUE; [[mainViewController searchDisplayController] setActive:NO animated: YES]; } }

我也尝试在 searchDisplayControllerDidEndSearch 方法中更改它,但这也无济于事。文本框......再次......为空。

编辑:实际上文本不是空的,但在动画消失后,自动完成表的结果仍然存在。所以它最终会变得更糟。

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller { NSLog(@"searchDisplayControllerDidEndSearch"); if (didSelectKeyword) { shouldReloadResults = FALSE; NSLog(@"keyword sdc didendsearch: %@", selectedKeyword); [[mainViewController keywordSearchBar] setText: selectedKeyword]; //

在这个方法中,我调用了另一个从 sqlite 中选择数据的方法。这部分工作正常。

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { NSLog(@"shouldReloadResults : %i", shouldReloadResults); 如果(应该重新加载结果) { NSLog(@"shouldReloadTableForSearchString: %@", searchString); [self GetKeywords: searchString:@"GB"]; NSLog(@"shouldReloadTableForSearchString vege"); 返回是; } 返回否; }

如果不清楚我的问题是什么,请询问我。我需要你的帮助。谢谢

【问题讨论】:

    标签: iphone objective-c uisearchdisplaycontroller searchdisplaycontroller


    【解决方案1】:

    你没试过吗: mainViewController.searchDisplayController.searchBar.text=selectedKeyword;

    您从搜索控制器访问搜索栏字段并更新其文本内容。

    【讨论】:

    • 仅供参考:searchDisplayController 在 iOS 8 中已弃用。看看UISearchController
    【解决方案2】:

    现在它起作用了,我不知道发生了什么。我想我在更改文本后试图停用搜索显示控制器。无论如何感谢您的帮助。我想我还尝试更改原始文本字段而不是属于搜索显示控制器的文本字段,它们是不同的(据我观察,内存中的地址不同)

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //NSLog(@"didSelectRowAtIndexPath"); if (allKeywords.count > 0) { didSelectKeyword = TRUE; self.selectedKeyword = (NSString *)[allKeywords objectAtIndex: indexPath.row]; NSLog(@"keyword didselectrow at idxp: %@", self.selectedKeyword); //shouldReloadResults = FALSE; [[mainViewController searchController] setActive:NO animated: YES]; [mainViewController.searchController.searchBar setText: self.selectedKeyword]; } }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      相关资源
      最近更新 更多