Tôi có một lớp gọi là ToolbarView là lớp con của UIView và về cơ bản tạo UIView có thanh UIToolbar biến mất/xuất hiện lại ở trên cùng. Tôi cũng có một lớp con của ToolbarView được gọi là DraggableToolbarView cho phép người dùng kéo chế độ xem xung quanh màn hình.Đang cố gắng triển khai quyền thừa kế đại biểu
Tôi cần tạo đại biểu cho ToolbarView để có thể thông báo cho đối tượng/lớp khác khi thanh công cụ xuất hiện lại và biến mất. Tôi cũng cần tạo đại biểu cho DraggableToolbarView
để tôi có thể thông báo cho đối tượng/lớp khác khi chế độ xem được kéo. DraggableToolbarViews delegate cũng sẽ cần thông báo cho một đối tượng/lớp khác khi thanh công cụ xuất hiện lại và biến mất.
Vì vậy, tôi quyết định thực hiện ToolbarViewDelegate, và đã DraggableToolbarViewDelegate kế thừa từ nó và có phương pháp riêng của nó như sau:
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];
}
...
}
Toolb arViewDelegate.h
@class ToolbarView;
@protocol ToolbarViewDelegate
@required
- (void) toolBarViewWillShowToolbar:(ToolbarView *)toolbarView;
- (void) toolBarViewWillHideToolbar:(ToolbarView *)toolbarView;
@end
DraggableToolbarView.h
#import "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];
...
Khi tôi làm điều này chỉ DraggableToolbarDelegate
phương pháp được đáp ứng. Tuy nhiên khi tôi cũng làm [drabbleToolbarView setToolbarViewDelegate:self]
nó hoạt động. Tôi đã cố gắng làm cho mỗi đại biểu một cách riêng biệt mà không có thừa kế và nó hoạt động tốt vì vậy tôi tin rằng vấn đề không phải là trong bất kỳ phần nào khác của mã.
Bất cứ ai cũng có thể biết tại sao? Tôi đã tìm ra bằng cách làm cho các giao thức kế thừa, tôi cũng sẽ không phải đặt ToolbarViewDelegate cho một đối tượng DraggableToolbar.
UPDATE: Thêm nhiều nhiều mã
Đánh giá cao bài đăng tuyệt vời đó. Hãy suy nghĩ tôi có thể chỉ cần giữ cho các đại biểu riêng biệt, không phải là một fan hâm mộ lớn của việc đặt tên. Bạn thường làm gì hoặc khuyên bạn nên làm gì? –
Vui vì tôi có thể giúp. Tôi không thấy bất cứ điều gì sai trái với việc ghi đè tài sản của đại biểu; nó làm cho cả hai mặt của trình dọn dẹp mã. –