Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 666
Mugunth Avatar asked Dec 04 '22 21:12

Mugunth


2 Answers

Just set the delegate to [self self].

like image 55
Morten Fast Avatar answered Dec 07 '22 10:12

Morten Fast


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.

like image 40
ughoavgfhw Avatar answered Dec 07 '22 09:12

ughoavgfhw