interface - Multi inheritance for IOS -
i want create class can inherit 2 custom class. have idea please? please see below example:
first class:
@interface uizoomableview : uiview { uitapgesturerecognizer *_tapgesturerecognizer; } and implementation:
- (void)ondoubletap:(uitapgesturerecognizer *)sender { cgsize newsize; cgpoint centerpoint = self.center; if ([self issmall]) { newsize = [self bigsize]; } else { newsize = [self smallsize]; } [uiview animatewithduration:0.3 animations:^{ self.size = newsize; self.center = centerpoint; }]; } second class:
@interface uidraggableview : uiview uipangesturerecognizer *_pangesturerecognizer; @end implementation:
- (void)handlepan:(uipangesturerecognizer*)sender { .. } i want create custom view can zoomable , draggable. have idea please? (without copy code..)
i think protocols want default value base classes? how can implement using protocol or protocols.
thanks response!
objective-c doesn't support multiple inheritance. use protocol, composition , message forwarding achieve same result.
a protocol defines set of methods object must implement (it's possible have optional methods too). composition technique of include reference object , calling object when it's functionality required. message forwarding mechanism allows objects pass messages onto other objects, example, object included via composition.
apple reference:
so, in case composition might solution, below example code
@interface classa : nsobject { } -(void)methoda; @end @interface classb : nsobject { } -(void)methodb; @end @interface myclass : nsobject { classa *a; classb *b; } -(id)initwitha:(classa *)ana b:(classb *)ab; -(void)methoda; -(void)methodb; @end @implementation myclass -(id)initwitha:(classa *)ana b:(classb *)ab { = ana ; b = ab ; } -(void)methoda { [a methoda] ; } -(void)methodb { [b methodb] ; } @end if don't want implement methods classa , classb in myclass, can use message forwarding in myclass handle method invocations. below works fine long classa , classb not have common methods.
@implementation myclass -(id)initwitha:(classa *)ana b:(classb *)ab { = ana ; b = ab ; } //this method called, when myclass can not handle method -(void)forwardinvocation:(nsinvocation *)aninvocation { if ([a respondstoselector:[aninvocation selector]]) [a invokewithtarget:someotherobject]; else if ([b respondstoselector:[aninvocation selector]]) [b invokewithtarget:someotherobject]; else [super forwardinvocation:aninvocation]; } @end
Comments
Post a Comment