I am losing my default UIScrollViewdelegate methods when I subclass it
I try to subclass the UScrollview but it ends up losing the default UIScrollview delegate method.
#import <UIKit/UIKit.h>
@protocol myscrollviewDelegate <NSObject>
-(void) myscrollview_return;
@end
@interface myscrollview : UIScrollView <UIScrollViewDelegate> {
id<myscrollviewDelegate> delegate;
}
@property(nonatomic, assign) id<myscrollviewDelegate> delegate;
@end
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
never get called when scroll.
what's wrong? Can I subclass the UIScrollview and add additonal delegate and at the same times keeping 开发者_如何学Gothe original delegates??
You are not adding a property, but overriding it, as UIScrollView already has a delegate property. When you set a delegate using the new property, the reference will be stored in the instance variable you added, not in the private instance variable of the original UIScrollView.
My theory is that the implementation of UIScrollView accesses the instance variable without using the property. I haven't verified it, but try not adding a new ivar and overriding the delegate property.
You can do this without creating a second delegate property.
First, make your delegate protocol inherit from UIScrollViewDelegate:
@protocol myscrollviewDelegate <NSObject, UIScrollViewDelegate>
Then, declare the delegate property in your header for your class:
@interface myscrollview : UIScrollView <UIScrollViewDelegate>
@property(nonatomic, assign) id<myscrollviewDelegate> delegate;
And the key is to not synthesize the property, but rather make it dynamic in your implementation file.
@implementation myscrollview
@dynamic delegate;
...
This is because You implement the delegate methods with id id delegate; I hope so so change the name of delegate. instead using delegate use other name like "delegateSomeClass" etc Now the delegates method of UIscrollView calls
hope it will clear :)
精彩评论