什么是委托和委托方法
委托和代理之间有什么区别?委托方法及其用途是什么?
What are the differences between Delegate & Delegate Methods and what are their uses?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
委托和代理之间有什么区别?委托方法及其用途是什么?
What are the differences between Delegate & Delegate Methods and what are their uses?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
这很难解释,但是委托代表另一个对象执行方法。当您在列表中选择一个项目时,表视图不知道要做什么。相反,它必须向委托对象询问一个问题,特别是 didSelectRowAtIndexPath。表视图知道的唯一信息是用户点击的部分和行。因此,表视图将这些信息提供给委托对象,本质上是说“嘿,用户点击了第 0 部分中的第 4 行。做一些事情。”
委托对象找到didSelectRowAtIndexPath方法并执行里面的代码。
许多不同的对象有许多委托方法。例如,文本字段对象本身无法执行任何操作。相反,它使用委托来执行操作。如果按屏幕键盘上的 Enter 键,文本字段会要求委托对象执行特定方法 textFieldShouldReturn。如果您为文本字段设置的委托没有 textFieldShouldReturn 方法,则当您按下 Enter 按钮时,文本字段将不知道要做什么。
这有道理吗?
It's hard to explain, but a delegate performs methods on behalf of another object. A Table View doesn't know what to do when you pick an item in the list. Instead, it has to ask the delegate object a question, specifically, didSelectRowAtIndexPath. The only information the tableview knows is which section and row the user tapped. So the table view gives this information to the delegate object by essentially saying that "Hey, the user tapped Row 4 in Section 0. Do something."
The delegate object finds the didSelectRowAtIndexPath method and executes the code inside.
There are lots of Delegate methods for many different objects. For instance, the Text Field object can't do anything on its own. Instead, it uses a delegate to perform actions. If you press the enter key on the on screen keyboard, the text field asks the delegate object to perform a specific method, textFieldShouldReturn. If the delegate you set for your text field does not have a textFieldShouldReturn method, the text field will not know what to do when you press the enter button.
Does this make sense?
委托是一个对象。委托方法是委托对象期望实现的方法。有些委托方法是必需的,而有些则不需要。在 IOS 中,大多数代表都应该遵守 Objective-C 协议;协议声明将告诉您哪些方法是可选的,哪些是必需的。
A delegate is an object. A delegate method is a method that the delegate object is expected to implement. Some delegate methods are required, while some are not. In IOS, most delegates are expected to conform to an Objective-C protocol; the protocol declaration will tell you which methods are optional and which are required.
委托只是对另一个对象的引用,委托方法是委托的方法。
委托方法实现回调机制,通常将发送者作为要调用的参数之一。
A delegate is simply a reference to another object and a delegate method is a method of the delegate.
A delegate method implements the callback mechanism which usually takes the sender as one of the parameter to be called.
所有 iOS 应用程序都使用委派。即使是基本的。它已经在他们给您的代码中可用。在其他语言中,您会使用诸如继承之类的东西。
就像在现实世界中一样,委托代表某人/某物,或者在本例中代表一个对象。
以 UI 对象为例,它们已经拥有执行任务的方法。但它们可以连接到您的 viewController (对象/自定义类),并且可以将责任委托给您的对象。
需要记住的几件事:
All iOS apps use Delegation. Even the basic ones. Its already available in the code they give you. In other languages you would use something like inheritance.
Just like in the real world a Delegate represents someone/something or in this case an object.
Take the UI objects for instance, they already have their methods to perform tasks. But they can be connected to your viewController (object/custom class) and they can delegate a responsibility to your object(s).
A few things to remember: