委托方法如何知道何时被调用 [英] How does a delegate method know when to be called
问题描述
我只是想知道一个委托方法怎么知道什么时候被调用?例如在 UITextFieldDelegate
协议中,当在文本字段中开始编辑时调用 textFieldDidBeginEditing:
方法(假设我实现了此方法)。
I'm just wondering how exactly does a delegate method know when to be called? For example in the UITextFieldDelegate
protocol the textFieldDidBeginEditing:
method is called when editing begins in the textfield (provided I implemented this method).
那么代码如何检测什么时候调用 textFieldDidBeginEditing:
?系统是否正在检查 textFieldDidBeginEditing:
是否已经实现,如果是运行该方法?有没有我看不到的东西?
So how exactly does the code to detect when to call textFieldDidBeginEditing:
? Does the system just check if textFieldDidBeginEditing:
is already implemented and if it is it runs that method? Is there something under the hood that I'm not seeing?
推荐答案
正确。
我不能保证Apple的框架代码如何实现,但非常普遍的缺点是:
I can't vouch for how Apple's framework code is implemented under the hood, but an exceedingly common refrain is:
if ([[self delegate] respondsToSelector:@selector(someInstance:didDoSomethingWith:)]) {
[[self delegate] someInstance:self didDoSomethingWith:foo];
}
这允许你有可选的委托方法,这似乎是你的问题。
This allows you to have optional delegate methods, which appears to be your question.
这篇关于委托方法如何知道何时被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!