BLOG

UITableView inherits UIScrollView and it uses it's own protocol (UITableViewDelegate) for 'delegate' property.
Usually using different delegate name like myDelegate in order to avoiding conflict with UIScrollView's delegate property when creating own view inherits UIScrollView. 

How can I use property name 'delegate' with my own protocol?
Check following codes. 

MyView.h

@protocol MyViewDelegate;

@interface MyView: UIScrollView
@property (nonatomic, weak) id<MyViewDelegate, UIScrollViewDelegate> delegate;
@end

@protocol MyViewDelegate <NSObject, UIScrollViewDelegate>
- (void)myViewDidFinish:(MyView *)sender;
@end


MyView.m

@implementation MyView

@synthesize delegate = _myViewDelegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [super setDelegate:self]; // do this if you want to implement UIScrollView's delegate
}

- (void)setDelegate:(id<MyViewDelegate, UIScrollViewDelegate>)delegate
{
    _myViewDelegate = delegate;
}

- (void)test
{
    if (_myViewDelegate && [_myViewDelegate respondsToSelector:@selector(myViewDidFinish:)])
        [_myViewDelegate myViewDidFinish:self];
}

#pragma mark UIScrollView Delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // Do what you want to do
}

@end


신고
Posted by xiles

Comment

This blog is powered by Daum / Skin design by xiles