수색…
소개
기존 iOS 컨트롤에 멀티 캐스팅 기능을 추가하기위한 패턴. 멀티 캐스팅을 추가하면 선명도와 코드 재사용이 향상됩니다.
모든 컨트롤에 대한 멀티 캐스트 대리자
위임자가 하나의 객체를 다른 객체로 전달하여 이러한 메시지를 여러 관찰자에게 멀티 캐스팅합니다.
1 단계 : - RRMulticastDelegate
NSObject
클래스 만들기
2 단계 : RRMulticastDelegate.h
파일의 다음 코드 구현
#import <Foundation/Foundation.h>
@interface RRMulticastDelegate : NSObject
{
//Handle multiple observers of delegate
NSMutableArray* _delegates;
}
// Delegate method implementation to the list of observers
- (void)addDelegate:(id)delegate;
- (void)removeDelegate:(id)delegate;
// Get multiple delegates
-(NSArray *)delegatesObjects;
@end
3 단계 : - RRMulticastDelegate.m
파일의 다음 코드 구현
#import "RRMulticastDelegate.h"
@implementation RRMulticastDelegate
- (id)init
{
if (self = [super init])
{
_delegates = [NSMutableArray array];
}
return self;
}
-(NSArray *)delegatesObjects
{
return _delegates;
}
- (void)removeDelegate:(id)delegate
{
if ([_delegates containsObject:delegate])
[_delegates removeObject:delegate];
}
- (void)addDelegate:(id)delegate
{
if (![_delegates containsObject:delegate])
[_delegates addObject:delegate];
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
if ([super respondsToSelector:aSelector])
return YES;
// if any of the delegates respond to this selector, return YES
for(id delegate in _delegates)
{
if (!delegate)
continue;
if ([delegate respondsToSelector:aSelector])
{
return YES;
}
}
return NO;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
// can this class create the sinature?
NSMethodSignature* signature = [super methodSignatureForSelector:aSelector];
// if not, try our delegates
if (!signature)
{
for(id delegate in _delegates)
{
if (!delegate)
continue;
if ([delegate respondsToSelector:aSelector])
{
return [delegate methodSignatureForSelector:aSelector];
}
}
}
return signature;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
// forward the invocation to every delegate
for(id delegate in _delegates)
{
if (!delegate)
continue;
if ([delegate respondsToSelector:[anInvocation selector]])
{
[anInvocation invokeWithTarget:delegate];
}
}
}
@end
4 단계 : - RRProperty
NSObject
카테고리 클래스 생성
5 단계 : - NSObject+RRProperty.h
파일의 다음 코드 구현
#import <Foundation/Foundation.h>
#import "RRMulticastDelegate.h"
@interface NSObject (RRProperty)<UITextFieldDelegate,UITableViewDataSource>
-(void)setObject:(id)block forKey:(NSString *)key;
-(id)objectForKey:(NSString *)key;
#pragma mark - Multicast Delegate
- (RRMulticastDelegate *)multicastDelegate;
- (RRMulticastDelegate *)multicastDatasource;
-(void)addDelegate:(id)delegate;
-(void)addDataSource:(id)datasource;
@end
6 단계 : NSObject+RRProperty.m
파일의 다음 코드 구현
#import "NSObject+RRProperty.h"
#import <objc/message.h>
#import <objc/runtime.h>
#pragma GCC diagnostic ignored "-Wprotocol"
static NSString *const MULTICASTDELEGATE = @"MULTICASTDELEGATE";
static NSString *const MULTICASTDATASOURCE = @"MULTICASTDATASOURCE";
@implementation NSObject (RRProperty)
-(void)setObject:(id)block forKey:(NSString *)key
{
objc_setAssociatedObject(self, (__bridge const void *)(key), block, OBJC_ASSOCIATION_RETAIN);
}
-(id)objectForKey:(NSString *)key
{
return objc_getAssociatedObject(self, (__bridge const void *)(key));
}
#pragma mark - Multicast Delegate
- (RRMulticastDelegate *)multicastDelegate
{
id multicastDelegate = [self objectForKey:MULTICASTDELEGATE];
if (multicastDelegate == nil) {
multicastDelegate = [[RRMulticastDelegate alloc] init];
[self setObject:multicastDelegate forKey:MULTICASTDELEGATE];
}
return multicastDelegate;
}
- (RRMulticastDelegate *)multicastDatasource
{
id multicastDatasource = [self objectForKey:MULTICASTDATASOURCE];
if (multicastDatasource == nil) {
multicastDatasource = [[RRMulticastDelegate alloc] init];
[self setObject:multicastDatasource forKey:MULTICASTDATASOURCE];
}
return multicastDatasource;
}
-(void)addDelegate:(id)delegate
{
[self.multicastDelegate addDelegate:delegate];
UITextField *text = (UITextField *) self;
text.delegate = self.multicastDelegate;
}
-(void)addDataSource:(id)datasource
{
[self.multicastDatasource addDelegate:datasource];
UITableView *text = (UITableView *) self;
text.dataSource = self.multicastDatasource;
}
@end
마지막으로 당신은 어떤 컨트롤에 대해서도 멀티 캐스트 dalegate를 사용할 수 있습니다 ...
예전 들어 ...
NSObject+RRProperty.h
파일에서 viewcontroller 클래스를 가져 NSObject+RRProperty.h
멀티 캐스트 위임 / 데이터 소스 를 설정 하는 메소드에 액세스하십시오.
UITextView *txtView = [[UITextView alloc]initWithFrame:txtframe];
[txtView addDelegate:self];
UITableView *tblView = [[UITableView alloc]initWithFrame:tblframe];
[tblView addDelegate:self];
[tblView addDataSource:self];
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow