Multicast delegates - multiple classes receiving notifications from user interface elements in iOS

I understand that user interface elements, such as UITextField, notify about client interactions / events through their delegate, which is defined as a class that supports the required protocol.

I often found that I want to receive UI event notifications in more than one class, so I would like to support multicast. For example, specify more than one delegate for a user interface control. I'm sure there is no iOS infrastructure feature that supports this. I was wondering if anyone came up with a decent solution to this problem?

+5
source share
3 answers

cocoa, - , .

, forwardInvocation: . , " ". forwardInvocation: , "" , .

, :

http://www.scottlogic.co.uk/blog/colin/2012/11/a-multicast-delegate-pattern-for-ios-controls/

+6

- :

@interface Delegator : NSObject - (void)addDelegate:(id<MyProtocol>)delegate; - (void)removeDelegate:(id<MyProtocol>)delegate; @end

NSHashTable.

: http://arielelkin.imtqy.com/articles/objective-c-multicast-delegate/

+2

, , ( - , 1- ). , UITextField -textFieldDidEndEditing:

-

1) :

@protocol TextControllerDelegate <NSObject>

@optional // Delegate protocols
- (void)textFieldDidEndEditing:(UITextField *)textField;

@end

2) @property (nonatomic, unsafe_unretained, readwrite) id <TextControllerDelegate> delegate;

3) - (void)textFieldDidEndEditing:(UITextField *)textField , [delegate textFieldDidEndEditing:textField]

:

1) implement the object of the first class, install the delegate on yourself (in the second class).

2) implementation method - (void)textFieldDidEndEditing:(UITextField *)textField

+1
source

All Articles