IOS CS193p - Why was the IBAction connection dragged to .m file and not to .h?

心已入冬 提交于 2019-12-07 02:48:35

问题


I noticed that in the calculator demo of Stanford CS193p course, the IBOutlet connection was dragged from the label to header file. However, the IBAction connection was directly created in the .m file. I tried reading through the documentation and searched on Google but couldn't find the reason behind this.

I would argue that IBAction digitPressed method should be declared in the header file as well (shouldn't it be part of the interface?).

I am obviously fairly new to Objective C (mostly worked in Java) so I am sure I am missing something basic.

Can anyone please explain?


回答1:


Declaring things in a class's header file makes them part of the public interface. This includes outlets and actions defined on a view controller.

Whether something ought to be made public is a design question. A good principal is: only if it has to be. With ViewControllers outlets and actions, more often than not, they don't have to be - and therefore shouldn't be - public.

What usually happens is that you make some guesses up front about what the public interface should contain, then (if you're a tidy developer) you take a look again when the implementation is matured and remove anything inessential.



来源:https://stackoverflow.com/questions/10003366/ios-cs193p-why-was-the-ibaction-connection-dragged-to-m-file-and-not-to-h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!