gpt4 book ai didi

iphone - UIVewController 的 UITextFieldDelegate 协议(protocol)

转载 作者:行者123 更新时间:2023-12-03 16:53:30 24 4
gpt4 key购买 nike

我完成了本教程 Your First iPhone Application效果很好。我的问题是关于教程中的一行,内容如下:

The view controller is also going to be the text field’s delegate; as such, it must adopt the UITextFieldDelegate protocol. To specify that a class adopts a protocol, in the interface add the name of the protocol in angle brackets (<>) after the name of the class from which your class inherits.

如果我从 View Controller 中删除协议(protocol),该方法仍然会被调用(收到消息)并且键盘会被关闭。我注意到 UITextFieldDelegate 的所有方法协议(protocol)是可选的。如果是这样,为什么要声明 View Controller 遵守协议(protocol)呢?我缺少什么? :-)

最佳答案

因为程序员喜欢让事情井井有条。即使协议(protocol)的所有方法都是可选的,如果您的类将用作委托(delegate),那么正式声明您的类实现该协议(protocol)仍然是一种很好的形式。委托(delegate)类可能还要求您的类实现该协议(protocol) - 这意味着如果您实现了这些方法但没有声明您实现了该协议(protocol),您将收到编译警告。

关于iphone - UIVewController 的 UITextFieldDelegate 协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1132922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com