objective-c - 我可以使用类方法作为委托(delegate)回调吗

标签 objective-c ios cocoa-touch

在类方法方法中,我声明如下:

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Hello" 
                                                   message:@"World"
                                                  delegate:self
                                         cancelButtonTitle:@"Cancel" 
                                         otherButtonTitles:@"Done", nil];

现在即使包含方法是类方法,编译器也不会提示 delegate:self。现在要成为委托(delegate)人,我必须在 UIAlertViewDelegate 协议(protocol)中实现该方法。

现在是否重要,如果方法是类方法,以便在用户与我的 AlertView 交互时被调用?

我问这个问题,因为我的类没有任何实例变量,所以我希望它只包含类方法。

最佳答案

我想分享一些我的发现:

  1. 无论我是像这样声明委托(delegate) delegate:self 还是 delegate:[MyClass class]

  2. 但是,我如何声明方法很重要:

    这行得通

    +(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        // Some Actions here
    }
    

    尽管以下“语法”是UIAlertViewDelegate 协议(protocol)中的声明,但它不起作用:

    // Mind the `-` sign at the start of the line.
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        // Some Actions here
    }
    

关于objective-c - 我可以使用类方法作为委托(delegate)回调吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8883521/

相关文章:

ios - 确定细胞的当前状态

objective-c - 让 __PRETTY_FUNCTION__ 只打印选择器的第一部分?

ios - 自动登录 Firebase 用户

ios - 将xcode 4.5.1项目移到其他文件夹

ios - 为什么我的 UITableViewCell 没有以任何样式显示 detailTextLabel?

iphone - 如何避免SIG_PIPE在IOS中崩溃

ios - 隐藏 UITextField 的光标

ios - UIPageViewController自动滚动问题

ios - 隐藏 UIDocumentInteractionController 的共享选项

objective-c - 在 Mac 应用程序中连接到 Internet