2012-10-15 13 views
6

mam zwyczaj klasy UIView GestureView. Mam przedłożoną deklarację dla tej klasy i jej delegata poniżej. Zaimportowałem plik GestureView.h do pliku .m. Działa to dobrze, ale system iOS wyświetla komunikat ostrzegawczy "Nie można znaleźć definicji protokołu dla GestureViewDelegate". Jeśli usuniemy zgłoszenie do przodu, otrzyma taki sam komunikat ostrzegawczy, co błąd. Nie chcę importować pliku GestureView.h z pliku ContainerViewController.h, ponieważ zazwyczaj importuję pliki w pliku .m. Czy ktoś mógłby wyjaśnić, co jest nie tak w podążaniu za strukturą klasy?iOS 5.0 Ostrzeżenie: Nie można odnaleźć definicję protokołu dla Delegata

ContainerViewController.h

#import <UIKit/UIKit.h> 

@class DividerView; 
@class GestureView; 
@protocol GestureViewDelegate; 

@interface ContainerViewController : UIViewController<GestureViewDelegate> 
    @property (strong, nonatomic) IBOutlet GestureView *topContentView; 
@end 

GestureView.h

#import <UIKit/UIKit.h> 

@protocol GestureViewDelegate; 

@interface GestureView : UIView 
    - (void)initialiseGestures:(id)delegate; 
@end 

@protocol GestureViewDelegate <NSObject> 
@required 
- (void)GestureView:(GestureView*)view handleSignleTap:(UITapGestureRecognizer*)recognizer; 
@end 
+0

Gdzie jest zdefiniowany twój protokół? – Moxy

+0

Możesz zobaczyć w opublikowanym przeze mnie kodzie! Jest w GestureView.h! – applefreak

+0

W 'ContainerViewController.h' spróbuj' #import GestureView.h' zamiast deklaracji forward – Moxy

Odpowiedz

21

Lubię że starasz się uniknąć importu w plikach nagłówkowych: Bardzo dobre praktyki. Jednak, aby naprawić błąd, możesz po prostu ulepszyć swój kod! Moim zdaniem nie jest konieczne, aby twoja klasa ContainerViewController deklarowała na zewnątrz, że obsługuje protokół GestureViewDelegate, więc powinieneś przenieść to do swojego pliku implementacji. Tak:

GestureView.h

#import <UIKit/UIKit.h> 


@protocol GestureViewDelegate; 

@interface GestureView : UIView 

- (void)initialiseGestures:(id <GestureViewDelegate>)delegate; 

@end 


@protocol GestureViewDelegate <NSObject> 
@required 

- (void)gestureView:(GestureView *)view handleSingleTap:(UITapGestureRecognizer *)recognizer; 

@end 

ContainerViewController.h

#import <UIKit/UIKit.h> 


@class GestureView; 

@interface CollectionViewController : UIViewController 

// this property is declared as readonly because external classes don't need to modify the value (I guessed seen as it was an IBOutlet) 
@property (strong, nonatomic, readonly) GestureView *topContentView; 

@end 

ContainerViewController.m

#import "ContainerViewController.h" 
#import "GestureView.h" 


// this private interface declares that GestureViewDelegate is supported 
@interface CollectionViewController() <GestureViewDelegate> 

// the view is redeclared in the implementation file as readwrite and IBOutlet 
@property (strong, nonatomic) IBOutlet GestureView *topContentView; 

@end 


@implementation ContainerViewController 

// your implementation code goes here 

@end 
+0

Awesome Neal! Rozumiem twoje rozwiązanie i ma to dla mnie sens, ale zastanawiam się, w jaki sposób zaimplementowano inne protokoły, takie jak UITableView-Delegate i Datasource? Możesz je zaimplementować w samej deklaracji pliku nagłówkowego! Czy nie jest tak, że możemy tworzyć nasze niestandardowe protokoły tak, że po zaimplementowaniu tego, musisz zastąpić @ wymagane metody? Jeśli ich nie wdrożymy, nie ma sensu posiadanie protokołów. Mogę po prostu całkowicie usunąć zmienną delegata protokołu z ContainerViewController i po prostu przekazać jej identyfikator podczas inicjalizacjiGestures. – applefreak

+0

Co więcej, kompilator iOS nie powinien w tym przypadku dawać komunikatu ostrzegawczego. Deklaracje forward mają na celu uniknięcie ostrzeżeń i błędów w kompilacji! Czy oni nie są? – applefreak

+0

Podczas tej operacji jest bardziej zrozumiałe, że obsługiwani delegaci są wymienieni w jednym miejscu - plik nagłówkowy, w którym wymieniono wszystkich standardowych delegatów. Np. W moim pliku '.h' mogę mieć' ', najlepiej chcę umieścić tam WSZYSTKICH delegatów. Jak to się robi? –

0

Spróbuj w ten sposób, i proszę o odpowiedź, czy to działa, czy nie.

GestureView.h

#import <UIKit/UIKit.h> 

@protocol GestureViewDelegate <NSObject> 
@required 
- (void)GestureView:(GestureView*)view handleSignleTap:(UITapGestureRecognizer*)recognizer; 
@end 

@interface GestureView : UIView 
    - (void)initialiseGestures:(id)delegate; 
@end 

ContainerView.h

#import <UIKit/UIKit.h> 

@class DividerView; 
@class GestureView; 
/*@protocol GestureViewDelegate;*/ //NO NEED TO WRITE THIS 

@interface ContainerViewController : UIViewController<GestureViewDelegate> 
    @property (strong, nonatomic) IBOutlet GestureView *topContentView; 
@end 
+0

Nie, to nie działa. Próbowałem już tego. Daje taki sam komunikat ostrzegawczy, jak ERROR! – applefreak

Powiązane problemy