iphone - UIVewController 的 UITextFieldDelegate 协议(protocol)

标签 iphone objective-c cocoa

我完成了本教程 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/

相关文章:

iphone - 如何在 iPhone 上进行地理编码(不是反向地理编码)?

iphone - 更改 iPhone 键盘上的 "enter"键文本?

iphone - iOS 将核心数据保存到服务器/云端

objective-c - NSView 与 Webview

高度约束发生变化时的 iOS Autolayout 问题

ios - dequeueReusableCellWithIdentifier : returns nil even after creating cell instance

macos - 在 Quartz 中使用 XOR 绘图

iphone - Localizable.strings 编译了两次?

objective-c - 特殊字符 NSString

ios - 两个 View 在垂直方向上不明确,Xcode 警告消息处理约束