我有一个叫做ToolbarView的类,它是UIView的一个子类,基本上创建了一个消失/重新出现的UIToolbar的UIView。我也有一个名为DraggableToolbarView的ToolbarView的子类,使用户可以在屏幕上拖动视图。试图实现委托继承
我需要为ToolbarView创建一个委托,以便它可以在工具栏重新出现和消失时通知另一个对象/类。我还需要为DraggableToolbarView
创建一个委托,以便在拖动视图时通知另一个对象/类。当工具栏重新出现并消失时,DraggableToolbarViews委托还需要通知另一个对象/类。
所以我决定实现ToolbarViewDelegate,并DraggableToolbarViewDelegate从它继承,并有自己的方法,像以下:
ToolbarView.h
#import <UIKit/UIKit.h>
@protocol ToolbarViewDelegate;
@interface ToolbarView : UIView <UIGestureRecognizerDelegate>
{
id <ToolbarViewDelegate> _toolbarViewDelegate;
}
@property(nonatomic, assign) id <ToolbarViewDelegate> toolbarViewDelegate;
@end
ToolbarView.m
#import "ToolbarView.h"
#import "ToolbarViewDelegate.h"
...
- (void) showBars
{
...
if (self.toolbarViewDelegate)
{
[self.toolbarViewDelegate toolbarViewWillShowToolbar:self];
}
...
}
- (void) hideBars
{
...
if (self.toolbarViewDelegate)
{
[self.toolbarViewDelegate toolbarViewWillHideToolbar:self];
}
...
}
工具箱arViewDelegate.h
@class ToolbarView;
@protocol ToolbarViewDelegate
@required
- (void) toolBarViewWillShowToolbar:(ToolbarView *)toolbarView;
- (void) toolBarViewWillHideToolbar:(ToolbarView *)toolbarView;
@end
DraggableToolbarView.h
#进口 “ToolbarView.h”
@protocol DraggableToolbarViewDelegate;
@interface DraggableToolbarView : ToolbarView
{
id <DraggableToolbarViewDelegate> _draggableToolbarViewDelegate;
}
@property(nonatomic, assign) id <DraggableToolbarViewDelegate> draggableToolbarViewDelegate;
@end
DraggableToolbarView.m
#import "DraggableToolbarView.h"
#import "DraggableToolbarViewDelegate.h"
...
- (void)drag:(UIPanGestureRecognizer *)sender
{
...
if (self.draggableToolbarViewDelegate)
{
[self.draggableToolbarViewDelegate draggableToolbarViewWillDrag:self];
}
...
}
...
DraggableToolbarViewDelegate.h
#import "ToolbarViewDelegate.h"
@class DraggableToolbarView;
@protocol DraggableToolbarViewDelegate <ToolbarViewDelegate>
@required
- (void) draggableToolbarViewWillDrag:(DraggableToolbarView *)draggableToolbarView;
@end
SomeViewController.h
#import <UIKit/UIKit.h>
#import "ToolbarViewDelegate.h"
#import "DraggableToolbarViewDelegate.h"
@interface SomeViewController : UIViewController <ToolbarViewDelegate, DraggableToolbarViewDelegate>
{
}
@end
SomeViewController.m
#import "DraggableToolbarView.h"
...
- (void) toolbarViewWillShowToolbar:(ToolbarView*)toolbarView
{
//NSLog(@"Toolbar Showed");
}
- (void) toolbarViewWillHideToolbar:(ToolbarView*)toolbarView
{
//NSLog(@"Toolbar Hidden");
}
- (void) draggableToolbarViewWillDrag:(DraggableToolbarView*)draggableToolbarView
{
//NSLog(@"Dragged");
}
...
[draggableToolbarView setDraggableToolbarViewDelegate:self];
...
当我这样做只是DraggableToolbarDelegate
方法响应。但是,当我也做[drabbleToolbarView setToolbarViewDelegate:self]
它的作品。我已经尝试了每个委托单独进行而没有继承,它工作正常,所以我相信问题不在代码的任何其他部分。
任何人都知道为什么?我想通过使协议继承,我不必为DraggableToolbar对象设置ToolbarViewDelegate。
更新:增加了更多的代码
欣赏这个伟大的职位。想想我可能会坚持让代表独立,而不是命名事物的忠实粉丝。你通常会做什么或建议做什么? –
很高兴我能帮到你。我没有看到覆盖委托属性的任何错误;它使代码的两边更清晰。 –