在类方法方法中,我声明如下:
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Hello"
message:@"World"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Done", nil];
现在即使包含方法是类方法,编译器也不会提示 delegate:self
。现在要成为委托(delegate)人,我必须在 UIAlertViewDelegate
协议(protocol)中实现该方法。
现在是否重要,如果方法是类方法,以便在用户与我的 AlertView 交互时被调用?
我问这个问题,因为我的类没有任何实例变量,所以我希望它只包含类方法。
最佳答案
我想分享一些我的发现:
无论我是像这样声明委托(delegate)
delegate:self
还是delegate:[MyClass class]
但是,我如何声明方法很重要:
这行得通
+(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/