Mugunth
Mugunth

Reputation: 14509

Setting delegates to "self" from a class method

Say, I want to call a UIActionSheet from a helper class method. I want the helper class (not the object) to be the delegate of this actionsheet. So I'm passing self to the delegate.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"MyTitle"
                                                         delegate:self
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:@"Delete" 
                                                otherButtonTitles:nil];

My helper class implements the delegate methods as class methods and everything works fine. But, I get a warning from the compiler that says, Incompatible pointer, sending Class when id is expected. I also tried [self class] and getting the same warning.

How can I avoid this warning?

Upvotes: 4

Views: 3145

Answers (4)

user2867846
user2867846

Reputation: 1

I solve it by adding < UIActionSheetDelegate > at the class .h file interface declaration

Upvotes: 0

Morten Fast
Morten Fast

Reputation: 6320

Just set the delegate to [self self].

Upvotes: 9

user756245
user756245

Reputation:

You won't quite the warning until you pass an object pointer to the method. It expects an id, where you give Class which is a typedef struct objc_class *Class;.

Upvotes: 0

ughoavgfhw
ughoavgfhw

Reputation: 39935

You can get rid of the warning by casting self to type id.

[[UIActionSheet alloc] initWithTitle:@"MyTitle"
                            delegate:(id<UIActionSheetDelegate>)self
                   cancelButtonTitle:nil
              destructiveButtonTitle:@"Delete" 
                   otherButtonTitles:nil];

This will tell the compiler to treat the value as an id which conforms to the UIActionSheetDelegate protocol.

Upvotes: 7

Related Questions