ios - Objective-C: Forward most messages to another object (at runtime) -
ios - Objective-C: Forward most messages to another object (at runtime) -
subclass of uiview
i have subclass myview
of uiview
.
this subclass has @property uiview * realview
.
whenever message sent myview
, want "forward it" self.realview
, excepted few messages.
for instance, in implementation of myview
, have override:
- (void)setbackgroundcolor:(uicolor *)color { [self.realview setbackgroundcolor:color] ; }
instead of overriding explicitly methods, can automatically, @ runtime?
exceptionsfor methods, want have explicit control. instance:
- (void)setframe:(cgrect)frame { /* stuff */ [super setframe:frame] ; }
instead of overriding explicitly methods, can automatically, @ runtime?
you implement -forwardinvocation:
method send unrecognized messages other object. -forwardinvocation
called whenever object doesn't implement selector that's passed sort of sec chance handle message. can override send message object (which pretty much nsproxy does), log messages, etc.
as @cobbal points out below, -forwardinvocation
help methods not implemented in superview, won't handle methods are implemented int superview because myview
inherits implementations of those. example, if want utilize uiview
proxy uibutton
, methods specific uibutton
can handled -forwardinvocation:
, defined uiview
cannot. in order behavior other inherited method, of course of study need override. in situations can around deriving myview
nsobject
or uiresponder
instead of uiview
, avoiding inherited uiview
implementations, if myview
needs real view you're stuck overriding each method.
if think it, it's hard imagine how goal met without explicitly overriding each inherited method. want forwards most messages, how can poor runtime tell ones want forwards , ones don't? can method given selector , phone call if finds one, or take action (like calling -forwardinvocation:
) if doesn't.
update: @robmayoff points out -forwardingtargetforselector:
, didn't occur me improve solution in case. still doesn't handle situation need redirect methods inherit superclass, though.
ios objective-c objective-c-runtime
Comments
Post a Comment