2011-06-08 77 views
1

我使用UIScrollView的自定義子類ImageScrollView。我需要知道用戶何時點擊滾動視圖,所以我創建了一個協議。我在我的RootViewController中執行協議,一切看起來都很好。當我建了,那裏有一個警告自定義協議的設置委託干擾UIScrollViewDelegate方法

班「ImageScrollView」不落實「TapDetectingImageViewDelegate」協議」。

ImageScrollView類檢查init方法後,我看到self.delegate = self;引起問題我宣佈自己是UIScrollView代表方法的代表,但我也是我自己的協議的代表(代表我的協議必須是RootViewController,而不是ImageScrollView

- (id)initWithFrame:(CGRect)frame{ 
     if ((self = [super initWithFrame:frame])) { 
     self.showsVerticalScrollIndicator = NO; 
     self.showsHorizontalScrollIndicator = NO; 
     self.bouncesZoom = YES; 
     self.decelerationRate = UIScrollViewDecelerationRateFast; 
     self.delegate = self;   
     } 
     return self; 
} 

你們知道我該如何解決這個問題嗎?或者告訴我的RootViewController該用戶已點擊UIScrollView/UIImage更好的主意。

在ImageScrollView.h協議聲明類的

@protocol TapDetectingImageViewDelegate <NSObject> 

@optional 
- (void)tapDetectingImageView:(ImageScrollView *)view gotSingleTapAtPoint:(CGPoint)tapPoint; 

@end 

頭文件實現在實現它

的.m類它

#import <UIKit/UIKit.h> 
#import "ImageScrollView.h" 

@interface AppleScrollViewViewController : UIViewController <UIScrollViewDelegate, TapDetectingImageViewDelegate>{ 

    UIScrollView *pagingScrollView; 
} 

-(void)configurePage:(ImageScrollView *)page forIndex:(NSUInteger)index; 
- (CGRect)frameForPageAtIndex:(NSUInteger)index; 
- (CGRect)frameForPagingScrollView; 
- (UIImage *)imageAtIndex:(NSUInteger)index; 
- (NSString *)imageNameAtIndex:(NSUInteger)index; 
- (CGSize)imageSizeAtIndex:(NSUInteger)index; 
- (NSArray *)imageData; 

@end 

協議方法

-(void)tapDetectingImageView:(ImageScrollView *)view gotSingleTapAtPoint:(CGPoint)tapPoint{ NSLog(@"SingleTap"); } 
+0

你可以發佈你的協議聲明和.h實現協議的類嗎?這聽起來像協議中方法聲明的簽名和實現不匹配。 – highlycaffeinated 2011-06-08 16:20:10

+0

當我發佈你問的東西時,發現我在我的課上實施了錯誤的協議方法。我實施了正確的方法並記錄了「單擊」。但我仍然收到警告。任何想法? – Canelo 2011-06-08 16:48:19

回答

1

如果你想在視圖控制器是其代表,那麼就不要內設置委託初始化程序。在創建ImageScrollView實例時,應將其設置爲視圖控制器。

ImageScrollView * aScrollView = [[ImageScrollView alloc] initWithFrame:aFrame]; 
aScrollView.delegate = self; 
[..] 

如果要創建它經由這個IB即重命名UIScrollView的類名稱,以ImageScrollView,你將不得不爲它創建的出口,然後在視圖控制器的viewDidLoad方法設置的委託。

但是,這種方法存在問題。如果ImageScrollViewUIScrollView的子類,顧名思義,滾動視圖的delegate屬性會丟失。你也應該考慮到這一點。

+0

這就是我現在的地方。我的協議現在可以工作,但滾動視圖的委託屬性會丟失。你知道任何解決方法或提示嗎? – Canelo 2011-06-08 17:08:58

+0

你是什麼意思它丟失了?你如何初始化對象?你如何設置代表? – 2011-06-08 17:10:02

+0

我在ImageScrollView類中通過它的委託方法(UIScrollView委託方法,因爲ImageScrollView是UIScrollView的子類)所做的所有配置/外觀不再有效。似乎如果我的協議確實工作,UIScrollView的沒有。 – Canelo 2011-06-08 17:17:12

1

您將具有在ImageScrollView.H文件RootViewController委託的聲明,

更改根委託rootDelegate的名稱;

在ImageScrollView.h

RootViewControllerDelegate* rootDelegate; 
    @property(nonatomic,assign) RootViewControllerDelegate* rootDelegate; 

在ImageScrollView.m文件

@synthesize rootDelegate; 
- (id)initWithFrame:(CGRect)frame{ 
     if ((self = [super initWithFrame:frame])) { 
     self.showsVerticalScrollIndicator = NO; 
     self.showsHorizontalScrollIndicator = NO; 
     self.bouncesZoom = YES; 
     self.decelerationRate = UIScrollViewDecelerationRateFast; 
     self.delegate = self;  
     // Root controller delegate. 
     self.rootDelegate = self;    
     } 
     return self; 
} 
+0

IM的問題在於ImageScrollView聲明協議,並且「self.delegate = self」假定自己必須實現該協議,這也是完全錯誤的。 ImageScrollView聲明協議,我的'RootViewController'必須實現它。這就是爲什麼xcode在ImageScrollView中引發警告的原因。任何想法? – Canelo 2011-06-08 16:57:54