我想讓我的腦袋回合Objective-C協議。我已經看過蘋果文檔以及我擁有的幾本書中的相關章節,但幾乎總是將協議看作是在將它們用作委託對象的接口方面進行定義。我瞭解創建協議定義的概念:需要簡單的協議示例?
@protocol PetProtocol <NSObject>
- (void)printCat;
- (void)printDog;
@end
我也明白,我符合一類的協議,該位:
@interface CustomController : UIViewController <PetProtocol> {
其次是實現所需的方法。
@implementation CustomController
- (void)printCat {
NSLog(@"CAT");
}
- (void)printDog {
NSLog(@"DOG");
}
我想我的問題是,如何使用該協議,似乎有點陌生,其中的方法實現打電話,printCat
和printDog
從CustomController
,任何人都可以點我在或者給我的一個非常簡單的例子使用這個協議或類似的?
非常感謝
加里。
如果你還在學習Objective-C,你可能想試試這個:http://www.lynda.com/home/DisplayCourse.aspx?lpk2 = 48369&srchtrk = index:6%0Alinktypeid:2%0Aq:iphone%20sdk%0Apage:1%0As:relevant%0Asa:true%0Aproducttypeid:4它的冗長,你必須付錢,但我用它,它絕對值得它 – livemac 2010-11-08 19:16:48
協議和代表不是一回事。協議很方便用於指定委託接口,但您不必使用協議。 – JeremyP 2010-11-08 21:34:26