ios - 如何使用取消按钮功能部署 iOS 7 mail.app 搜索栏

标签 ios objective-c uisearchbar resignfirstresponder

我正在寻找创建一个 searchBar,虽然我在我的表格 View 上有点工作,但仍然需要一些努力才能让它 100% 完美。

引用 iOS 7 Mail.app,我该如何部署类似的东西?因此,在您单击搜索栏之前,搜索栏不会显示“取消”按钮,而取消按钮会取消搜索并将表格返回到原来的位置。

我有以下代码:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    _fetchedResultsController = nil;
    NSError *error;

    if (![[self fetchedResultsController] performFetch:&error])
    {
        NSLog(@"Error in search %@, %@", error, [error userInfo]);
    }

    else
    {
        [self.timelineTableView reloadData];
        [self.timelineSearchBar resignFirstResponder];
        [self.noResultsLabel setHidden:_fetchedResultsController.fetchedObjects.count > 0];
    }
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.timelineSearchBar resignFirstResponder];
    self.timelineSearchBar.hidden = YES;
    [self.timelineTableView reloadData];
    [self viewDidLoad];
}

所以有了这个,我基本上希望 searchBar 在表格 View 中一直可见,并且不显示取消按钮,直到用户开始输入。如果他们执行搜索并产生结果或没有结果,我希望取消按钮:
  • 搜索栏的 ResignFirstResponder
  • 移除取消按钮
  • 将 TableView 恢复到搜索前的状态。

  • 谢谢

    最佳答案

    UISearchBarDelegate 中使用以下两种方法并做这样的事情:

    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
    {
        [searchBar setShowsCancelButton:YES animated:YES];
    }
    
    - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
    {
        [searchBar setShowsCancelButton:NO animated:YES];
    }
    

    如果您希望搜索看起来更有响应性,您可以将发布的代码移至 searchBar:textDidChange:委托(delegate)方法,然后只使用 searchBarSearchButtonClicked:做一个[searchBar resignFirstResponder] :
    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
    {
        [searchBar resignFirstResponder];
    }
    
    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
    
        _fetchedResultsController = nil;
        NSError *error;
    
        if (![[self fetchedResultsController] performFetch:&error])
        {
            NSLog(@"Error in search %@, %@", error, [error userInfo]);
        }
    
        else
        {
            [self.timelineTableView reloadData];
            [self.noResultsLabel setHidden:_fetchedResultsController.fetchedObjects.count > 0];
        }
    }
    

    此外,您可以使用 searchBarCancelButtonClicked:还要从搜索栏中退出第一响应者,然后通过调用委托(delegate) searchBar:textDidChange: 更新您的表格 View :
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
    {
        [searchBar setText:@""];
        [self searchBar:searchBar textDidChange:@""];
        [searchBar resignFirstResponder];
    }
    

    关于ios - 如何使用取消按钮功能部署 iOS 7 mail.app 搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20471310/

    相关文章:

    ios - 如何在 Objective-C 中创建新对象并分配属性?

    iphone - 单击 UISearchbar (searchDisplayController) 时应用程序崩溃

    iphone - 如何在 Notes.app 中复制 UISearchBar 滚动行为?

    ios - 如何在 ui 搜索栏中的 x 按钮操作事件上 resignfirstResponder?

    ios - 改变widgetactivedisplaymode iOS

    iphone - [mapView addOverlay :overlayPolygon]; 时,集合在枚举时发生了变异

    ios - 捕获组正则表达式 NSRegularExpression iOS

    ios - 使用 SVGKit 渲染带有嵌入式 Base64 编码的 png 图像的 SVG 需要相当大的内存

    ios - 翻页时隐藏导航栏

    ios - 我可以只在标签栏项目中显示文本并更改样式和位置吗