代表子类? [英] Subclass a Delegate?

查看:73
本文介绍了代表子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为的工具栏视图,它是 UIView 的子类,基本上创建一个 UIView 在顶部有消失/重现的 UIToolbar 。我也有一个 ToolbarView 的子类 DraggableToolbarView ,使用户可以在屏幕上拖动视图。

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.

我需要为 ToolbarView 创建一个委托,以便它可以通知另一个对象/类,当工具栏重新出现和消失时。我还需要为 DraggableToolbarView 创建一个委托,所以我可以通过拖动视图时另一个对象/类。

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.

目前,我为每个创建一个单独的委托,但我想知道是否有一个更好的模式为此?可能为 ToolbarView 实现一个委托,并将 DraggableToolbarView 中的委托方法列为可选?

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?

推荐答案

如果你为代理方法创建一个协议(总是一个好主意),你可以有另一个协议采用第一个。这创建了一个类似继承的关系:

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

这篇关于代表子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆