从类方法将委托设置为"self"

Mug*_*nth 4 iphone delegates class objective-c ios

说,我想从帮助器类方法调用UIActionSheet.我希望辅助类(而不是对象)成为此操作表的委托.所以我将自己传递给代表.

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"MyTitle"
                                                         delegate:self
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:@"Delete" 
                                                otherButtonTitles:nil];
Run Code Online (Sandbox Code Playgroud)

我的帮助器类将委托方法实现为类方法,一切正常.但是,我从编译器那里得到一个警告:不兼容的指针,当id期望时发送Class.我也尝试过[自习]并得到同样的警告.

我该如何避免这种警告?

Mor*_*ast 9

只需将委托设置为[self self].


ugh*_*fhw 7

您可以通过将self转换为id类型来消除警告.

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

这将告诉编译器将该值视为id符合UIActionSheetDelegate协议的值.