////VIEWCONTROLLER.M//Observertrampoline////Created by Rob Napier on 9/7/11.//Copyright (c) __mycompanyname__. All rights reserved.//#import "ViewController.h"#import "RNObserverManager.h"@protocolMyProtocol <NSObject>@optional- (void) dosomething;- (void) Dodo;@end@interfaceMyclass:nsobject <MyProtocol>@end@implementationMyClass- (void) dosomething {NSLog (@"dosomething:%@", self);}@end@interfaceMyclass1:nsobject@end@implementationMyClass1- (void) Dodo1 {NSLog (@"dodo1:%@", self);}- (void) Dodo {NSLog (@"Dodo to go.");}@end@implementationViewcontroller//@synthesize Observermanager=trampoline_;- (void) viewdidload {[Super viewdidload]; //This allows you to do something, such as a class that adheres to a protocol that can directly tune the protocol to execute the something written in that class.Nsset*observers = [Nsset setwithobjects:[myclassNew],[myclass1New], nil]; Self.observermanager=[[Rnobservermanager alloc] Initwithprotocol:@protocol(MyProtocol) observers:observers]; [Self.observermanager dosomething]; [Self.observermanager Dodo];}@end
Method of implementation
#import "RNObserverManager.h"@interfaceRnobservermanager () @property (nonatomic,ReadOnly, strong) Nsmutableset *observers, @property (Nonatomic,ReadOnly, strong) Protocol *protocol;@end@implementationRnobservermanager//Protocol Collection- (ID) Initwithprotocol: (Protocol *) Protocol observers: (Nsset*) Observers {if(self =[Super Init]) {_protocol=Protocol; _observers=[Nsmutableset setwithset:observers]; } returnSelf ;}- (void) Addobserver: (ID) Observer {NSASSERT ([observer ConformsToProtocol:self.protocol],@"Observer must conform to protocol."); [Self.observers addobject:observer];}- (void) Removeobserver: (ID) Observer {[Self.observers removeobject:observer];}//NSObject--------//This function gives the overloaded party the opportunity to throw a signature of a function, followed by the forwardinvocation: to execute. -(Nsmethodsignature *) Methodsignatureforselector: (SEL) sel {nsmethodsignature*result=[Super Methodsignatureforselector:sel]; if(Result) {returnresult; } //Look for a required method structObjc_method_description desc =protocol_getmethoddescription (Self.protocol, SEL, yes, yes); if(Desc.name = =NULL) { //couldn ' t find it. Maybe it ' s optionaldesc =protocol_getmethoddescription (Self.protocol, SEL, NO, YES); } if(Desc.name = =NULL) { //couldn ' t find it. Raise nsinvalidargumentexception[self Doesnotrecognizeselector:sel]; returnNil; } return[Nsmethodsignature signatureWithObjCTypes:desc.types];}//true execution from Methodsignatureforselector: The returned nsmethodsignature. In this function, nsinvocation can be forwarded to multiple objects several times, which is also a flexible way. (Forwardingtargetforselector can only be turned to an object in the form of selector)- (void) Forwardinvocation: (Nsinvocation *) Invocation {SEL selector=[invocation selector]; for(IDResponderinchself.observers) {if([responder Respondstoselector:selector]) {[Invocation settarget:responder]; [Invocation invoke]; } }}
Reference: http://www.cnblogs.com/biosli/p/NSObject_inherit_2.html
http://blog.csdn.net/yiyaaixuexi/article/details/8970734
Ios-runtime-A class has the same method as the protocol, according to the protocol name.