searchDisplayController

searchDisplayController

我想实现一个UIControllerDisplayController这样的控制器来显示搜索结果,并添加一些日志来弄清楚逻辑。

看完日志后,我对UISearchDisplayController如何窃取UISearchBar事件感到非常好奇。

UISearchDisplayController的工作方式无需将UISearchBarDelegate设置为其自身。而且UISearchBarDelegate在外面同时工作!

以下是日志:


  searchBarShouldBeginEditing:
  
  searchBarTextDidBeginEditing:
  
  searchDisplayControllerWillBeginSearch:
  
  searchDisplayController:didLoadSearchResultsTableView:
  
  searchBar:shouldChangeTextInRange:replacementText:
  
  searchBar:textDidChange:
  
  searchDisplayController:shouldReloadTableForSearchString:
  
  searchDisplayController:willShowSearchResultsTableView:
  
  searchDisplayController:didShowSearchResultsTableView:
  
  searchBarCancelButtonClicked:
  
  searchBarShouldEndEditing:
  
  searchBarTextDidEndEditing:
  
  searchDisplayController:willHideSearchResultsTableView:
  
  searchDisplayController:didHideSearchResultsTableView:
  
  searchBarShouldBeginEditing:
  
  searchBarTextDidBeginEditing:
  
  searchDisplayControllerWillBeginSearch:
  
  searchBar:shouldChangeTextInRange:replacementText:
  
  searchBar:textDidChange:
  
  searchDisplayController:shouldReloadTableForSearchString:
  
  searchDisplayController:willShowSearchResultsTableView:
  
  searchDisplayController:didShowSearchResultsTableView:
  
  searchBar:shouldChangeTextInRange:replacementText:
  
  searchBarSearchButtonClicked:
  
  searchBarShouldEndEditing:
  
  searchBarTextDidEndEditing:
  
  searchBarCancelButtonClicked:
  
  searchDisplayController:willHideSearchResultsTableView:
  
  searchDisplayController:didHideSearchResultsTableView:

最佳答案

好吧,我想通了。

您只需将UISearchBar委托属性添加到KVO观察中,然后将其实现为委托代理即可。

示例代码:



    if (searchBar.delegate) {
        self.searchBarDelegate = searchBar.delegate;
    }

    searchBar.delegate = self;

    [searchBar addObserver:self forKeyPath:@"delegate" options:NSKeyValueObservingOptionNew context:kSearchBarKVOContext];




#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == kSearchBarKVOContext) {
        [self updateSearchBarDelegate];
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}




- (void)updateSearchBarDelegate
{
    if (self.searchBar.delegate == self) {
        self.searchBarDelegate = nil;
    } else {
        self.searchBarDelegate = self.searchBar.delegate;
        self.searchBar.delegate = self;
    }
}




#pragma mark - NSObject Overrides
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
    NSMethodSignature *result = [super methodSignatureForSelector:aSelector];

    if (!result) {
        NSObject *obj = (NSObject *)self.searchBarDelegate;

        result = [obj methodSignatureForSelector:aSelector];
    }

    return result;
}

- (void)forwardInvocation:(NSInvocation *)anInvocation
{
    if ([self.searchBarDelegate respondsToSelector:[anInvocation selector]]) {
        [anInvocation invokeWithTarget:self.searchBarDelegate];
    } else {
        [super forwardInvocation:anInvocation];
    }
}

- (BOOL)respondsToSelector:(SEL)aSelector {
    return ([super respondsToSelector:aSelector] ||
            [self.searchBarDelegate respondsToSelector:aSelector]);
}

关于ios - UISearchDisplayController如何窃取UISearchBar事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23531613/

10-12 03:35