有没有办法让多个对象侦听UISearchbBar实例的void委托方法?多个代表收听uisearchbar的一个实例
例如,如何UISearchDisplayController知道搜索条具有其文本串中改变:
– searchDisplayController:shouldReloadTableForSearchString:
而在同一时间实例中,搜索显示控制器可搜索的代表表视图控制器酒吧,并知道文字是否改变?
有没有办法让多个对象侦听UISearchbBar实例的void委托方法?多个代表收听uisearchbar的一个实例
例如,如何UISearchDisplayController知道搜索条具有其文本串中改变:
– searchDisplayController:shouldReloadTableForSearchString:
而在同一时间实例中,搜索显示控制器可搜索的代表表视图控制器酒吧,并知道文字是否改变?
@interface MultiplexingSearchBarDelegate : NSObject<UISearchBarDelegate> {
NSMutableArray* delegates;
}
- (void) addDelegate: (id) theDelegate;
- (void) removeDelegate: (id) theDelegate;
@end
@implementation MultiplexingSearchBarDelegate
- (id) init {
if ((self = [super init])) {
delegates = [[NSMutableArray alloc] initWithCapacity: 16];
}
}
- (void) dealloc {
[delegates release];
[super dealloc]
}
- (void) addDelegate: (id) theDelegate {
@synchronized(delegates) {
if (theDelegate && ! [delegates containsObject: theDelegate]) {
[delegates addObject: theDelegate];
}
}
}
- (void) removeDelegate: (id) theDelegate {
@synchronized(delegates) {
if (theDelegate && [delegates containsObject: theDelegate]) {
[delegates removeObject: theDelegate];
}
}
}
//add your UISearchBarDelegate methods here, following a pattern like this
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
@synchronized(delegates) {
for (id<UISearchBarDelegate> theDelegate in delegates) {
[theDelegate searchBar:searchBar textDidChange:searchText];
}
}
}
@end
然后,只需设置一个MultiplexingSearchBarDelegate
为UISearchBar
的代表,并直接添加委托给MultiplexingSearchBarDelegate
而不是向UISearchBar
。
只有一个对象可以是委托并接收委托方法调用。然而,委托可以通过你写的委托协议来通知许多对象。你可能需要重构一下。