PDA

View Full Version : Using @protocol




beardedpig
Jan 19, 2009, 10:13 AM
I have written a class that is a subclass of UIImageView and has some custom methods in it.

I now find I need yet another UIImageView subclass along the same lines but with a different version of some of the methods.

I have been looking at using @Protocol but suspect as usual I haven't grasped the bleedin concept!

My thoughts were this:

Create the inteface file of the custom class like with the protocol in it like so:


#import <UIKit/UIKit.h>

@interface CustomImageView : UIImageView {

}

@protocol CustomImageViewMethods

-(void) doubleTap;

@end


I read that I do not need to create an implementation file as the class subclassing this will do so.

So I then create a class of type CustomImageView like so


@interface myImageView : CustomClassView <CustomImageViewMethods> {
}


and subsequently get more errors than you can wave a stick at!

Am I so far off beam I should be thrashed with a large iceberg lettuce?



d.ma
Jan 19, 2009, 11:02 AM
Just out of curiosity, what are the errors, specifically?
Also, does your myImageView class have a call to doubleTap the exact type and argument set that the protocol states?

PhoneyDeveloper
Jan 20, 2009, 09:23 AM
Couple random comments.

@protocol is related to delegation. If you're not setting up a delegate then your protocol doesn't make sense.

@protocol goes outside the @interface, not inside as you've shown it. The protocol is like a class declaration, although it has no code. A given class, the delegate, would adopt the protocol and implement it.

beardedpig
Jan 20, 2009, 12:20 PM
Hi chaps,

yes I realise my error not only in protocol usage but also in the fact that all I needed to do was create a custom class of the required type and then subclass this!

live and learn....live and learn :)