【问题标题】:Text in search bar disappears when search button on keyboard is clicked in iPhone在 iPhone 中单击键盘上的搜索按钮时,搜索栏中的文本会消失
【发布时间】:2014-07-21 03:28:15
【问题描述】:

在 iPhone 的搜索栏中输入文字后,当点击键盘上的搜索按钮时,文字会消失。我添加了<UISearchDisplayDelegate, UISearchBarDelegate>BOOL isSearching;

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    isSearching = YES;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
     NSLog(@"Text change - %d",isSearching);
     NSString *str = searchBar.text;
     self.searchDisplayController.searchBar.text = str;
     //Remove all objects first.
     [filteredArray removeAllObjects];

     if([searchText length] != 0) {
         isSearching = YES;
         [self searchTableList];
     }
     else {
         isSearching = NO;
     }
     [self.tableView1 reloadData];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  NSLog(@"Cancel clicked");
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
   [self searchTableList];
   [searchBar resignFirstResponder];
}
- (void)searchTableList {
    searchString = searchBar.text;

    for (NSString *tempStr in tableData) {
        NSComparisonResult result = [tempStr compare:searchString options:   (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
        if (result == NSOrderedSame) {
            [filteredArray addObject:tempStr];
        }
    }
}

【问题讨论】:

  • 我没有看到任何可能导致该行为的内容...您的代码中是否还有其他内容正在操纵搜索栏?
  • 不,我没有使用任何其他搜索栏方法

标签: ios iphone searchbar


【解决方案1】:

斯威夫特 4.0:

我遇到了同样的问题,但我通过定义 UISearchBarDelegate 方法解决了,即 searchBarTextDidEndEditing

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {

}

无需在方法中编写任何代码。 希望这个回答对你有所帮助。

【讨论】:

    【解决方案2】:

    我在 UISearchController 上遇到了同样的问题。
    我阻止搜索控制器清除将其存储在字符串中的文本,然后在停用控制器后将存储的文本分配给搜索栏。

    目标 C:

    -(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
        NSString *text = searchBar.text;
        [self.searchController setActive:NO animated:YES];
        self.searchController.searchBar.text = text;
    }
    

    斯威夫特 2.X:

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        let text = searchBar.text
        searchController.active = false
        searchController.searchBar.text = text
    }
    

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      斯威夫特 5

      func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
          searchBar.resignFirstResponder()
      }
      

      【讨论】:

        猜你喜欢
        • 2019-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-08
        • 1970-01-01
        • 1970-01-01
        • 2015-07-07
        相关资源
        最近更新 更多