Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclass a Delegate?

I have a class called ToolbarView which is a subclass of UIView and basically creates a UIView that has a disappearing / reappearing UIToolbar on top. I also have a subclass of ToolbarView called DraggableToolbarView which enables the user to drag the view around the screen.

I need to create a delegate for ToolbarView so it can notify another object / class of when the toolbar reappears and disappears. I also need to create a delegate for DraggableToolbarView so I can notify another object / class when the view is dragged.

Currently, I have create a separate delegate for each, but I am wondering if there is a better pattern for this? Maybe implement one delegate for ToolbarView, and list the delegate methods from DraggableToolbarView as optional? Or is there a way to subclass a delegate?

What is the best / cleanest way to accomplish this?

like image 581
Ser Pounce Avatar asked Mar 15 '26 16:03

Ser Pounce


2 Answers

If you create a protocol for your delegate methods (always a good idea anyway), you can have another protocol adopt the first. That sets up an inheritance-like relationship:

@protocol ToolbarViewDelegate
// some methods
@end

@protocol DraggableToolbarViewDelegate <ToolBarViewDelegate>
// additional methods
@end
like image 51
Caleb Avatar answered Mar 17 '26 06:03

Caleb


Yes, you can have inheriting protocols:

@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
like image 24
Richard J. Ross III Avatar answered Mar 17 '26 04:03

Richard J. Ross III



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!