Start Developing iOS Apps Today

Start Developing iOS Apps Today

Introduction

Review the Source Code

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:

  • An instance of the UIApplication class, called the application object.
    • The application object manages the app event loop and coordinates other high-level app behaviors. This class, defined in the UIKit framework, doesn’t require you to write any additional code to get it to do its job.
  • An instance of the XYZAppDelegate class, called the app delegate.
    • Xcode created this class for you as part of setting up the Empty Application template. The app delegate creates the window where your app’s content is drawn and provides a place to respond to state transitions within the app. This window is where you write your custom app-level code.

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.

Structuring an App

1. App Development Process

Defining the Interaction

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.

2. Designing a User Interface

The View Hierarchy

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.

Building an Interface Using Views

Although each view has its own specific function, UIKit views can be grouped into seven general categories:

Category

Purpose

Examples

image: ../Art/views_content.png

Content

Display a particular type of content, such as an image or text.

Image view, label

image: ../Art/views_collections.png

Collections

Display collections or groups of views.

Collection view, table view

image: ../Art/views_controls.png

Controls

Perform actions or display information.

Button, slider, switch

image: ../Art/views_bars.png

Bars

Navigate, or perform actions.

Toolbar, navigation bar, tab bar

image: ../Art/views_input.png

Input

Receive user input text.

Search bar, text view

image: ../Art/views_containers_2x.png

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

Use Inspectors to Configure Views

  • image: ../Art/inspector_file.pngFile. Lets you specify general information about the storyboard.

  • image: ../Art/inspector_quick_help.pngQuick Help. Provides useful documentation about an object.

  • image: ../Art/inspector_identity.pngIdentity. Lets you specify a custom class for your object and define its accessibility attributes.

  • image: ../Art/inspector_attributes.pngAttributes. Lets you customize visual attributes of an object.

  • image: ../Art/inspector_size.pngSize. Lets you specify an object’s size and Auto Layout attributes.

  • image: ../Art/inspector_connections.pngConnections. Lets you create connections between your interface and source code.

In particular, you’ll use the Attributes inspector to configure your views, the Identity inspector to configure your view controllers, and the Connections inspector to create connections between your views and view controllers.

3. Defining the Interaction

View Controllers

In an iOS app, you use a view controller (UIViewController) to manage a content view with its hierarchy of subviews.

image: ../Art/view_layer_objects_2x.png

Controls

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.

Navigation Controllers

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.

Implementing an App

1. Using Design Patterns

MVC

image: ../Art/ModelViewController_2x.png

Target-Action

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.

image: ../Art/target_action_2x.png

Delegation

Delegation is a simple and powerful pattern in which one object in an app acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it.

image: ../Art/delegation_2x.png

2. Working with Foundation

The Foundation framework includes value classes representing basic data types such as strings and numbers, as well as collection classes for storing other objects.

Value Objects

A value object is an object that encapsulates a primitive value (of a C data type) and provides services related to that value.

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';

Collection Objects

Most collection objects in Objective-C code are instances of one of the basic collection classes— 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.

Next Steps

iOS Technologies

User Interface

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.

Games

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.

Data

Core Data. The Core Data framework manages an app’s data model. With Core Data, you create model objects, known as managed objects. You manage relationships between those objects and make changes to the data through the framework. Core Data takes advantage of the built-in SQLite technology to store and manage data efficiently.

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.

Media

AV Foundation. AV Foundation is one of several frameworks that you can use to play and create time-based audiovisual media.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值