Subclass of the delegate?

I have a class called ToolbarView, which is a subclass UIViewand basically creates UIView, which has fading / re-appearing UIToolbaron top. I also have a subclass ToolbarViewcalled DraggableToolbarViewthat allows the user to drag the view around the screen.

I need to create a delegate for ToolbarViewso that it can notify another object / class when the toolbar reappears and disappears. I also need to create a delegate for DraggableToolbarViewso that I can notify another object / class when dragging the view.

I am currently creating a separate delegate for each, but I wonder if there is a better template for this? Maybe implement one delegate for ToolbarViewand list delegate methods from DraggableToolbarViewas optional? Or is there a way to subclass a delegate?

What is the best / cleanest way to do this?

+2
source share
3 answers

If you are creating a protocol for your delegation methods (always a good idea), you can use a different protocol for the first. This establishes a relation similar to inheritance:

@protocol ToolbarViewDelegate
// some methods
@end

@protocol DraggableToolbarViewDelegate <ToolBarViewDelegate>
// additional methods
@end
+5
source

Yes, you may have protocols inheriting:

@protocol Proto1 

@reqired
-(void) somethingHappened:(id) sender;

@optional
-(void) somethingElseHappened:(id) sender;

@end

@protocol Proto2<Proto1>

// this now contains all of the method signatures found in proto1, with the addition of new ones!

-(void) somethingSpecialHappened:(id) sender;

@end
+4
source

, .

Consider UITextViewwhich is a subclass UIScrollView. Each of them has its own protocol of delegates, which is responsible for responding to a specific set of messages. As long as you think about visibility and drag as separate issues, letting various objects handle their delegation seems logical.

0
source

All Articles