Introduction of Xcode

39
Using Xcode

Transcript of Introduction of Xcode

Using Xcode

Introduction to XCode

• This tutorial will walk you through Xcode, a software development tool for Apple’s iOS applications– We will explore its different parts and

their functions– This tutorial will teach you how to use

Xcode, but not how to build an app.• To build an app, you need to know Objective-

C.

Useful Terms

• Objective-C: the programming language used to write iOS applications, based on C and using object oriented programming methods

• Object: a collection of code with its data and ways to manipulate that data

Useful Terms

• View: how your program presents information to the user

• Model: how your data is represented inside of your application

App Templates, Pt 2

Tabbed: Like the iPod app, with lots of different ways to view the same database items

Utility: Like the weather app, a main view and a configuration view

Empty: You build everything from scratch

Starting an App

Choose the name you want for your app

Click ‘Next’

Choose a folder in which to save your app

Finally, choose your device

Writing a universal iOS app is more difficult than writing for just one device

This is what your screen looks like now….

The main parts we’ll be focusing on…1. Navigator Panel

3. Libraries

2. Inspector Panel

Navigator Panel

The Classes folder contains two objects:

- The App Delegate

- The View Controller

The extensions:- .h = header, defines

object- .m= main/body-.xib= XML interface

builder

The App Delegate

• Handles starting and ending your app

• Serves as a go-between between iOS and your app– Hands off control to your code after

starting

The View Controller

• Handles everything that shows up on screen

• Handles all the info that the onscreen objects need to display themselves

• Translates between the view and the model

• Responds to user input and uses that to change model data– Responsible for updating view from the

model

To help visualize…

From developer.apple.com

XML Interface Builder

This is where you lay out graphic views

The view controller knows how to talk to the objects that have been created here

Lots of formatting options

Supporting Files, Pt. 1

These are system files

.plist = property list

Appname-Info.plist = contains info about your app for the iOS. It is an XML file that includes the options you put on your app (which device, etc.)

InfoPlist.strings = helps to internationalize your app

- Language translation cues

- .strings is any text

Supporting Files, Pt. 2

Main.m = low level. Starts app and gives to the App Delegate. Never change this file.

.pch = pre-compiled header

Appname-Prefix.pch = generated by the system to speed up builds

Frameworks

Frameworks contains a lot of already written code provided by the system

- A library of code bits- Related to the Libraries menu on the right of

Xcode

UIKit = contains code for everything that interfaces with the user (views)

Foundation = alll the components used to build the model

CoreGraphics = handles drawing on the screen

Inspector Panel

• This area contains utilities panels that let you change properties of your app’s view objects, like:

• Colors• Sizes• Images• Button actions

Libraries

• Different goodies depending on which icon you click– From left to right:

• File templates• Code snippets• View Objects• Media/Images

Model, View, Controller (MVC)iOS applications follows

the MVC design pattern.

• Model: Represents the business logic of your application

• View: Represents what the user sees in the device

• Controller: Acts as a mediator between the Model and View. There should not be any direct conversation between the View and the Model. The Controller updates the View based on any changes in the underlying Model. If the user enters or updates any information in the View, the changes are reflected in the Model with the help of the Controller.

How does a View or Model interact with the Controller?

• Views can interact with the Controller with the help of targets or delegates.

• Whenever the user interacts with a View, for example by touching a button, the View can set the Controller associated with it as the target of the user’s action. Thus the Controller can decide on further actions to be taken. We will see how this can be achieved in the later part of this tutorial.

• Views can also delegate some of the actions to the Controller by setting the Controller as its delegate.

MVC

• The Model notifies the Controller of any data changes, and in turn, the Controller updates the data in the Views. The View can then notify the Controller of actions the user performed and the Controller will either update the Model if necessary or retrieve any requested data.

Outlet And ActionsOutlet: • ViewController talks to View by using

Outlet. Any object (UILabel, UIButton, UIImage, UIView etc) in View can have an Outlet connection to ViewController. Outlet is used as @property in ViewController which means that:

• you can set something (like Update UILabel's text, Set background image of a UIView etc.) of an object by using outlet.

• you can get something from an object (like current value of UIStepper, current font size of a NSAttributedString etc.)

Action:•   View pass on messages about view to

ViewController by using Action (Or in technical terms ViewController set itself as Target for any Action in View). Action is a Method in ViewController (unlike Outlet which is @property in ViewController).

• Whenever something (any Event) happens to an object (like UIbutton is tapped) then Action pass on message to ViewController. Action (or Action method) can do something after receiving the message.Note: Action can be set only by UIControl's child object; means you can't set Action for UILabel, UIView etc.

Application Life Cycle

• applicationWillResignActive:—Lets you know that your app is transitioning away from being the foreground app. Use this method to put your app into a quiescent state.

• applicationDidEnterBackground:—Lets you know that your app is now running in the background and may be suspended at any time.

• applicationWillEnterForeground:—Lets you know that your app is moving out of the background and back into the foreground, but that it is not yet active.

• applicationWillTerminate:—Lets you know that your app is being terminated. This method is not called if your app is suspended.

Application State• Not running

• Inactive

• Active

• Background

• Suspended

View controller

• Connect the view and the controller with

IBOutlet

View Controller Life Cycle

• - (void)viewDidLoad;

• - (void)viewWillAppear:(BOOL)animated;

• - (void)viewDidAppear:(BOOL)animated;

• - (void)viewWillDisappear:(BOOL)animated;

• - (void)viewDidDisappear:(BOOL)animated:

• -(void) viewDidUnload;

UINavigationController• The UINavigationController class

implements a specialized view controller that manages the navigation of hierarchical content. This navigation interface makes it possible to present your data efficiently and makes it easier for the user to navigate that content.

• A navigation controller object manages the currently displayed screens using the navigation stack

TableView Control

• Table View is one of the common UI elements in iOS apps

• Most apps, in some ways, make use of Table View to display list of data

• The “UITableViewDelegate” and “UITableViewDataSource” are known as protocol in Objective-C. Basically, in order to display data in Table View, we have to conform to the requirements defined in the protocols and implement all the mandatory methods.

UITableViewDelegate

• UITableViewDelegate, deals with the appearance of the UITableView. Optional methods of the protocols let you manage the height of a table row, configure section headings and footers, re-order table cells, etc.

UITableViewDataSource

• We’ll use the table view to present a list of recipes. So how do you tell UITableView the list of data to display? UITableViewDataSource is the answer. It’s the link between your data and the table view. The UITableViewDataSource protocol declares two required methods

• tableView:cellForRowAtIndexPath • tableView:numberOfRowsInSection