Start Developing iOS Apps Today
The Empty Application template comes with a few existing source code files that set up the app environment. Most of the work is done by the UIApplicationMain function, which is automatically called in your project’s main.m source file. The UIApplicationMain function creates an application object that sets up the infrastructure for your app to work with the iOS system. This includes creating a run loop that delivers input events to your app.
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
The call to UIApplicationMain creates two important initial components of your app:
Here’s how the application object and app delegate interact. As your app starts up, the application object calls defined methods on the app delegate to give your custom code a chance to do its job—that’s where the interesting behavior for an app is executed.
The app delegate interface contains a single property: window. With this property the app delegate keeps track of the window in which all of your app content is drawn.
iOS apps are based on event-driven programming. In event-driven programming, the flow of the app is determined by events: system events or user actions. The user performs actions on the interface, which trigger events in the app. These events result in the execution of the app’s logic and manipulation of its data. The app’s response to user action is then reflected back in the interface.
The view hierarchy defines the layout of views relative to other views. Within that hierarchy, view instances enclosed within a view are called subviews, and the parent view that encloses a view is referred to as its superview. Even though a view instance can have multiple subviews, it can have only one superview.
At the top of the view hierarchy is the window object. Represented by an instance of the UIWindow class, a window serves as the basic container into which you can add your view objects for display onscreen. By itself, a window doesn’t display any content. To display content, you add a content view (with its hierarchy of subviews) to the window.
Although each view has its own specific function, UIKit views can be grouped into seven general categories:
Category |
Purpose |
Examples |
---|---|---|
Content |
Display a particular type of content, such as an image or text. |
Image view, label |
Collections |
Display collections or groups of views. |
Collection view, table view |
Controls |
Perform actions or display information. |
Button, slider, switch |
Bars |
Navigate, or perform actions. |
Toolbar, navigation bar, tab bar |
Input |
Receive user input text. |
Search bar, text view |
Containers |
Serve as containers for other views. |
View, scroll view |
Modal |
Interrupt the regular flow, allow a user perform some kind of action. |
Action sheet, alert view |
File. Lets you specify general information about the storyboard.
Quick Help. Provides useful documentation about an object.
Identity. Lets you specify a custom class for your object and define its accessibility attributes.
Attributes. Lets you customize visual attributes of an object.
Size. Lets you specify an object’s size and Auto Layout attributes.
Connections. Lets you create connections between your interface and source code.
In an iOS app, you use a view controller (UIViewController) to manage a content view with its hierarchy of subviews.
A control is a user interface object such as a button, slider, or switch that users manipulate to interact with content, provide input, navigate within an app, and perform other actions that you define.
When a user interacts with a control, a control event is created. A control event represents various physical gestures that users can make on controls
There are three general categories of event types:
Touch and drag events. Touch and drag events occur when a user interacts with a control with a touch or drag. There are several available touch event stages. When a user initially touches a finger on a button, for example, the Touch Down Inside event is triggered; if the user drags out of the button, the respective drag events are triggered. Touch Up Inside is sent when the user lifts a finger off the button while still within the bounds of the button’s edges. If the user has dragged a finger outside the button before lifting the finger, effectively canceling the touch, the Touch Up Outside event is triggered.
Editing events. Editing events occur when a user edits a text field.
Value-changed events. Value-changed events occur when a user manipulates a control, causing it to emit a series of different values.
A navigation controller manages transitions backward and forward through a series of view controllers, such as when a user navigates through email accounts, inbox messages, and individual emails in the iOS Mail app.
The set of view controllers managed by a particular navigation controller is called its navigation stack. The navigation stack is a last-in, first-out collection of custom view controller objects. The first item added to the stack becomes the root view controller and is never popped off the stack. Other view controllers can be pushed on or popped off the navigation stack.
Navigation Controller is also responsible to presenting the navigation bar —the view at the top of the screen that provides context about the user’s place in the navigation hierarchy—which contains a back button and other buttons you can customize. Every view controller that’s added to the navigation stack presents this navigation bar. You are responsible for configuring the navigation bar.
Target-action is a conceptually simple design in which one object sends a message to another object when a specific event occurs. Theaction message is a selector defined in source code, and thetarget—the object that receives the message—is an object capable of performing the action, typically a view controller. The object that sends the action message is usually a control—such as a button, slider, or switch—that can trigger an event in response to user interaction such as tap, drag, or value change.
For example, imagine that you want to restore default settings in your app whenever a user taps the Restore Defaults button (which you create in your user interface). First, you implement an action,restoreDefaults:
, to perform the logic to restore default settings. Next, you register the button’s Touch Up Inside event to send therestoreDefaults:
action method to the view controller that implements that method.
You create a value object from data of the primitive type (and then perhaps pass it in a method parameter). In your code, you later access the encapsulated data from the object.
int n = 5; // Value assigned to primitive type
NSNumber *numberObject = [NSNumber numberWithInt:n]; // Value object created from primitive type
int y = [numberObject intValue]; // Encapsulated value obtained from value object (y == n)
Most value classes create their instances by declaring both initializers and class factory methods.Class factory methods—implemented by a class as a convenience for clients—combine allocation and initialization in one step and return the created object.
// Create the string "My String" plus carriage return.
NSString *myString = @"My String\n";
// Create the formatted string "1 String".
NSString *anotherString = [NSString stringWithFormat:@"%d %@", 1, @"String"];
// Create an Objective-C string from a C string.
NSString *fromCString = [NSString stringWithCString:"A C string" encoding:NSUTF8StringEncoding];
NSNumber *myIntValue = @32;
NSNumber *myDoubleValue = @3.22346432;
NSNumber *myBoolValue = @YES;
NSNumber *myCharValue = @'V';
NSArray
,
NSSet
, and
NSDictionary
.
Any object you add to a collection will be kept alive at least as long as the collection is kept alive. That’s because collection classes use strong references to keep track of their contents.
NSArray
and NSMutableArray
—An array is an ordered collection of objects. You access an object by specifying its position (that is, its index) in the array. The first element in an array is at index 0 (zero).
NSSet
and NSMutableSet
—A set stores an unordered collection of objects, with each object occurring only once. You generally access objects in the set by applying tests or filters to objects in the set.
NSDictionary
and NSMutableDictionary
—A dictionary stores its entries as key-value pairs; the key is a unique identifier, usually a string, and the value is the object you want to store. You access this object by specifying the key.
UIKit. The UIKit framework provides classes to create a touch-based user interface. Because all iOS apps are based on UIKit, you can’t ship an app without this framework. UIKit provides the infrastructure for drawing to the screen, handling events, and creating common user interface elements.
Core Graphics. Core Graphics—a low-level, C-based framework—is the workhorse for handling high-quality vector graphics, path-based drawing, transformations, images, data management, and more.
Core Animation. Core Animation is a technology that allows you to make advanced animations and visual effects. UIKit provides animations that are built on top of the Core Animation technology.
Game Kit. The Game Kit framework provides leaderboards, achievements, and other features to add to your iOS game.
Sprite Kit. The Sprite Kit framework provides graphics support for animating arbitrary textured images, orsprites.
OpenGL ES. OpenGL ES is a low-level framework that provides tools to support hardware-accelerated 2D and 3D drawing.
Game Controller. The Game Controller framework makes it easy to find controllers connected to a Mac or iOS device.
Foundation. The Foundation framework defines a base layer of Objective-C classes. In addition to providing a set of useful primitive object classes, this framework introduces several paradigms that define behaviors not covered by the Objective-C language.
AV Foundation. AV Foundation is one of several frameworks that you can use to play and create time-based audiovisual media.