ios &不需要在标头中声明的 obj-c 方法
我在 Lynda.com 上查看了一些有关 iphone 开发的视频。
这个人正在向选择器添加自定义数据,但为了添加数据,他没有在头文件中声明方法。他正在检查文档中需要哪些方法,并将这些方法声明复制粘贴到他的控制器类中。
例如这个方法
-(int) numberOfComponentsInPickerView: (UIPickerView *) pickerView
为什么我们不需要在头文件中声明这些方法?
如果这些方法属于选取器类,为什么我们要在控制器类中声明它们,而不是简单地在选取器 IBOutlet 实例中调用它们?
TIA
I was checking some video at Lynda.com about iphone development.
The guy was adding custom data to a picker, but to add the data he was not declaring the methods in the header file. He was checking which nethods he needed on the documentation and copying pasting those methods declarations in his controller class.
For exampe this method
-(int) numberOfComponentsInPickerView: (UIPickerView *) pickerView
Why doesn't we need to declare those methods on the header file?
If those methods pertain to the picker class, why do we declare them in the controller class instead of simply calling them in the picker IBOutlet instance?
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您在标头中声明您遵循某个协议时,您实质上是在表示您同意实现该协议中定义的方法。
我敢打赌他在标题中添加了类似的内容:
这意味着他正在实现 UIPickerViewDataSource 协议。
因此,通过实现 UIPickerViewDataSource 协议,您就暗示了这些方法,因此不需要对它们进行原型设计。
When you declare in your header that you follow a certain protocol, you are essentially saying that you agree to implement the methods defined in the protocol.
I'll bet he added something like this to the header:
That means that he is implementing the UIPickerViewDataSource protocol.
So, by implementing the
UIPickerViewDataSource
protocol, you are implying those methods, therefore, they do not need to be prototyped.您引用的方法不属于
UIPickerView
类,而是属于UIPickerViewDataSource
协议。您的控制器充当选择器的“助手”,因此如果选择器需要确定应显示多少个组件,则它会调用您的控制器。您通常不会自己调用此方法。作为另一个答案 指出,通过声明您实现协议,该协议的方法被隐式声明。通过将
添加到您的界面,您基本上是在说“我可以充当选取器视图的数据源,并且我已准备好任何想要调用以下方法的选取器:协议中声明”。The method you quoted does not belong to the
UIPickerView
class, but rather to theUIPickerViewDataSource
protocol. Your controller acts as a "helper" for the picker, so the picker is calling your controller if it needs to figure out how many components it should display. You are usually not calling this method yourself.As another answer pointed out, by declaring that you implement the protocol, the methods of that protocol are implicitly declared. By adding
<UIPickerViewDataSource>
to your interface, you're basically saying "I can act as a data source for a picker view and I'm ready for any picker that wants to call the methods that are declared in the protocol".