![](/img/trans.png)
[英]How to set text in UISearchBar without activating UISearchDisplayController
[英]How UISearchDisplayController steals UISearchBar events
我想實現像控制器一樣的UISearchDisplayController來顯示搜索結果,並添加一些日志以弄清楚邏輯。
看完日志后,我對UISearchDisplayController如何竊取UISearchBar事件感到非常好奇。
UISearchDisplayController的工作方式無需將UISearchBarDelegate設置為其自身。 而且UISearchBarDelegate在外面同時工作!
以下是日志:
searchBarShouldBeginEditing:
searchBarTextDidBeginEditing:
searchDisplayControllerWillBeginSearch:
searchDisplayController:didLoadSearchResultsTableView:
搜索欄:shouldChangeTextInRange:replacementText:
搜索欄:textDidChange:
searchDisplayController:shouldReloadTableForSearchString:
searchDisplayController:willShowSearchResultsTableView:
searchDisplayController:didShowSearchResultsTableView:
searchBarCancelButtonClicked:
searchBarShouldEndEditing:
searchBarTextDidEndEditing:
searchDisplayController:willHideSearchResultsTableView:
searchDisplayController:didHideSearchResultsTableView:
searchBarShouldBeginEditing:
searchBarTextDidBeginEditing:
searchDisplayControllerWillBeginSearch:
搜索欄:shouldChangeTextInRange:replacementText:
搜索欄:textDidChange:
searchDisplayController:shouldReloadTableForSearchString:
searchDisplayController:willShowSearchResultsTableView:
searchDisplayController:didShowSearchResultsTableView:
搜索欄: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]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.