English 中文(简体)
下级代表?
原标题:Subclass a Delegate?

我有一个叫做 ToolbarView 的分类,它是 >UIV 的子分类,基本上创造了一个 UIView , 上面有一个消失/重新出现 UIToolbar 的分类。我还有一个叫做 ToolbarView 的子类,叫做 DragggableToolbarView 的分类,使用户能够拖动屏幕周围的视图。

我需要为 ToolbarView 创建一个代表, 以便它能够通知另一个对象/ 类工具栏重新出现和消失的时间。 我还要为 DragggableToolbarView 创建一个代表, 以便在拖动视图时通知另一个对象/ 类 。

目前,我为每个代表团另设一个代表,但我想知道是否有更好的模式?也许可以设置一个代表来代表 ToolbarView ,并从 DraggableToolbarView 中列出代表方法,作为可选办法;或者有办法将代表分组吗?

实现这一点的最佳/最清洁的方法是什么?

最佳回答

如果您为代表方法创建一个协议(无论如何总是一个好主意),您可以另设一个协议,通过第一个协议。这建立起一种类似继承的关系:

@protocol ToolbarViewDelegate
// some methods
@end

@protocol DraggableToolbarViewDelegate <ToolBarViewDelegate>
// additional methods
@end
问题回答

是的, 您可以有 < code> 继承 协议 :

@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

我觉得你做得对

UITextView UIScrollView 的子类。 每个人有自己的代表协议, 负责对特定的一组信件作出反应。 只要您认为能见度和拖曳是另外的问题, 允许不同对象处理自己的代表团似乎合乎逻辑 。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签