【问题标题】:searchBarTextDidBeginEditing delegate method called two timessearchBarTextDidBeginEditing 委托方法调用了两次
【发布时间】:2013-11-22 07:19:40
【问题描述】:

我正在调试我的代码,其中 UISearchBar 的委托方法 searchBarTextDidBeginEditing: 每次点击搜索栏(位于导航栏中)时都会被调用两次。

奇怪的是,只有这个委托方法被调用了两次。其他的在整个过程中只调用一次,这是正确的行为。

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    // called only once
    return YES;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    // called twice every time
    [searchBar setShowsCancelButton:YES animated:YES];
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    // called only once
    [searchBar setShowsCancelButton:NO animated:YES];    
    return YES;
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    // called only once        
}

知道可能出了什么问题吗?

UISeachrBar 设置在 Storyboard 中,具有正确连接的插座,尽管它没有添加到任何视图中,并且在特定视图控制器的 viewDidLoad 中,以下行将搜索栏添加到导航栏:

self.searchDisplayController.displaysSearchBarInNavigationBar = YES;

我正在使用 Xcode 5.0.1 并在 iOS 7.0.3 模拟器中运行代码。

【问题讨论】:

  • 是否有可能有两个搜索栏在彼此之上?
  • 我也遇到了这种情况,这似乎是一个 iOS 错误。我用 UITableView 和 UISearchDisplayController 创建了一个空白项目,在导航栏中显示 UISearchBar。以下委托方法被调用两次: -searchBarTextDidBeginEditing: -searchBar:textDidChange: -searchBarCancelButtonClicked:
  • 是的,这很奇怪。我也有同样的问题,但我太贪心了,无法以我的评分开始赏金)))
  • 我在 Xcode 8.2.1 上也有这个问题:D

标签: ios iphone objective-c cocoa-touch ios7


【解决方案1】:

我遇到了同样的问题,并且深入研究了一些。

在我的例子中,我有一个UISearchDisplayController 的子类,它本身作为UISearchDisplayDelegate,而UISearchBarDelegate 作为它的UISearchBar

事实证明,问题在于UISearchDisplayController implements the following methods that collide with theUISearchBarDelegate`协议:

- (void)searchBar:(id)arg1 textDidChange:(id)arg2;
- (void)searchBarCancelButtonClicked:(id)arg1;
- (void)searchBarResultsListButtonClicked:(id)arg1;
- (void)searchBarSearchButtonClicked:(id)arg1;
- (void)searchBarTextDidBeginEditing:(id)arg1;

这意味着如果您将 UISearchDisplayController 设为它自己的 UISearchBar 的委托,这些方法将被调用两次。

【讨论】:

    【解决方案2】:

    我发现,如果您取消设置 searchBar 委托并只保留 searchDisplayController 委托,则根本不会再调用该方法。所以我能想出的唯一解决方法是把它放在你的 searchBarTextDidBeginEditing 和 searchBarTextDidEndEditing 的开头。

    static NSDate *lastInvocation;
    
    if ([[NSDate date] timeIntervalSinceDate:lastInvocation] < 0.1f) {
        lastInvocation = [NSDate date];
        return;
    } else {
        lastInvocation = [NSDate date];
    }
    

    【讨论】:

      【解决方案3】:

      我找到了这个解决方案: [searchBar setShowsCancelButton:NO 动画:YES]; [searchBar resignFirstResponder]; 但有趣的是,经过两轮测试,我删除了它,代码只调用了一次,而不是两次

      【讨论】:

        【解决方案4】:

        我遇到了 searchBarSearchButtonClicked 的问题:方法被调用了两次。 通过调用 [searchBar resignFirstResponder] 解决了问题;

            #pragma mark - UISearchViewDelegate methods
            - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
             // Do things
             [searchBar resignFirstResponder];
            }
        

        【讨论】:

          猜你喜欢
          • 2011-02-07
          • 1970-01-01
          • 1970-01-01
          • 2014-08-23
          • 1970-01-01
          • 2018-06-27
          • 2014-07-03
          • 2013-07-10
          • 2015-04-11
          相关资源
          最近更新 更多