我可以使用类方法作为委托回调 [英] Can I use a class method as a delegate callback
问题描述
在类方法方法中,我声明如下:
In a class method method I declare the following:
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Hello"
message:@"World"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Done", nil];
现在即使contains方法是一个类方法,编译器也不会抱怨 delegate:self
。现在要成为委托,我必须实现 UIAlertViewDelegate
协议中的方法。
Now even though the containing method is a class method, the compiler does not complain about the delegate:self
. Now to be the delegate I have to implement the method in UIAlertViewDelegate
protocol.
方法是否为类方法,以便在用户与我的AlertView交互时调用?
Does it now matter, if the method is a class method or not, in order to be called when the user interacts with my AlertView?
我问这个问题,因为我的类没有任何实例
I am asking this question, because my class does not have any instance variables so I would prefer it to contain class methods only.
推荐答案
首先,我假设通过静态方法,你的意思是类方法(用 +
而不是 -
)声明。
First, I assume by "static" methods, you mean class methods (declared with +
instead of -
).
编译器不抱怨关于Objective-C并不总是告诉你很多。这真的意味着,你已经说服了编译器一切应该(或至少可以)在运行时工作。
"The compiler does not complain" about Objective-C doesn't always tell you much. All it really means is that you have convinced the compiler that everything should (or at least could) work at runtime.
如果你作为一个委托提供的对象响应正确的消息,编译器不会关心它是一个类(运行时也不会)。在这种情况下,您将从类方法提供 self
。它就像你输入 [MyClass class]
。
In the end, if the object you provide as a delegate responds to the right messages, the compiler won't care that it's a class (and neither will the runtime). In this case, you are providing self
from a class method. It is as though you typed [MyClass class]
.
我认为这可能是有问题的, em>应该做 - 但这可能在你的问题的边界之外。
I think it's probably questionable whether this is what you should do--but that's probably outside the bounds of your question.
这篇关于我可以使用类方法作为委托回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!