In the last article, we discussed the importance of splash screens for iPhone programs
If you download the Creative-Commons-licensed class from the previous article, you can begin coding with splash screens immediately. However, we're also going to take the time in this article and the next to discuss some of SplashView's code, particularly paying attention to coding elements that we didn't cover in iPhone in Action.
All About the Header File
This article is going to be all about the header file. To be honest, header files aren't that interesting; there's a reason we very rarely included them in our code snippets in iPhone in Action. However, the SplashView header contains one design element that we didn't discuss in our book, so it deserves some discussion here.
At the start of the header file, before the actual @class definition, you'll find a typedefed enum:
typedef enum {
SplashViewAnimationNone,
SplashViewAnimationSlideLeft,
SplashViewAnimationFade,
} SplashViewAnimation;
A typedef is an entirely standard C feature. It allows you to define a standard variable type with a new name. It's used a lot in the SDK. For example, an NSTimeInterval is really a double with a typedef.
An enum is another standardC feature, the "enumeration constant." It's essentially a set of #defines, with incrementing value, starting with 0.
When you put these together, you can define a new sort of variable which responds to one of several constants. This methodology is also used throughout the SDK. For example, the UITabBarSystemItem variable, which lets you choose a standard look-and-feel for a tab bar button, is a typedefed enum; there are many more examples.
Thus, if you want to mimic the way the SDK works when you're defining new classes (and generally you should), you want to use a typedefed enum when you're defining a property that can be set to one of a limited number of options. In this case, we wanted to offer an option between a few animation types (with the possibility of more in the future), and so a typedefed enum seemed like an obvious choice.
After you've written your typedef, you can use it to define variables like normal:
SplashViewAnimation animation;
And that typedef is the only element of particular importance in the SplashView header file.
However, we'll also call your attention to the use of properties. There are lots of them, most notably for animation, animationDelay, delay, and touchAllowed, our "public" instance variables. Any class is ultimately a combination of variables and methods, wrapped together; properties let you include those variables in your class while doing almost no extra work.
In this example, the properties are defined in the header file, synthesized in the source code file, and defined (for default values) in the init method. No other code is written, because the synthesis takes care of all our getters and setters for us.
After this rather short look at the header file, we'll get into the guts of SplashView, by looking at the source code file, on Friday.

Comments