在默认搜索栏中搜索字符串为空时启用搜索按钮

Gan*_*thy 24 iphone objective-c uisearchbar ios

在我的应用程序中我使用默认IOS搜索栏的搜索功能,如果我放置一些字符串进行搜索工作正常但在第一次搜索后我需要显示整个数据源(原始内容.)我的功能是如果搜索字符串是为空它将显示整个数据源.我的问题是如果我在默认搜索栏中将搜索字符串设为空,搜索按钮会自动进入隐藏状态.我需要启用搜索按钮,即使字符串是空的.请指导我解决此问题.

在此输入图像描述

谢谢.

小智 32

实际上你可以searchBar.enablesReturnKeyAutomatically = NO;在iOS 7+上设置测试


iPa*_*tel 23

请编写以下代码可能对您有所帮助:)如果您有空字符串,此代码显示搜索按钮.

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
{
    [self.searchBar setShowsCancelButton:YES animated:YES];
    self.tblView.allowsSelection = NO;
    self.tblView.scrollEnabled = NO;

    UITextField *searchBarTextField = nil;
    for (UIView *subview in self.searchBar.subviews)
    {
        if ([subview isKindOfClass:[UITextField class]])
        {
            searchBarTextField = (UITextField *)subview;
            break;
        }
    }
    searchBarTextField.enablesReturnKeyAutomatically = NO;
}
Run Code Online (Sandbox Code Playgroud)

试试吧:)谢谢:)