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

1

6

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?

Rentschler answered 4/4, 2012 at 0:28 Comment(0)
Y
8

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.

Yunyunfei answered 4/4, 2012 at 2:47 Comment(5)
Being able to declare the IBOutlets in the .m file and link them in IB is fairly new. Demos and tutorials haven't all caught up. It is definitely the better solution, and avoids very bad practices like modifying another object's IBOutlets (which you should never do).Domingo
@danh, can you explain why outlets and actions don't have to be public ? How does the View access these in the controller if these are not public ?Rentschler
ViewController can be a confusing name if you've been schooled in MVC. It's not, as you might think, a pairing of two peers in that acronym. It's better to think of it as the thing that controls a view. In that sense, it makes more sense to keep one's view private. This isn't a rule, or course, just a common practice. View is a property of view controller, and subclasses you create often have additional subviews that they control. That control is usually fairly absolute, in that they don't share that control with others. Let me google a bit for a good reference, and I'll post it here.Yunyunfei
developer.apple.com/library/ios/#featuredarticles/… was the best thing i could find with a quick google.Yunyunfei
@Rentschler I'm new to objective c but as I understand it, methods defined only in your .m file can technically be called by any client without error, it's just that the client shouldn't expect those "private" methods to implement the same interface over time. So, the concept of public/private in objective c is used to capture and communicate the "intended use" of any given method, not "enforce" its use. If somebody knows this to be untrue, please elaborate.Stewartstewed

© 2022 - 2024 — McMap. All rights reserved.