iOS Development - Patterns, the application's life cycle, views

fortunajugglerΚινητά – Ασύρματες Τεχνολογίες

19 Ιουλ 2012 (πριν από 4 χρόνια και 11 μήνες)

442 εμφανίσεις

Patterns, the application's life cycle, views
Patterns

Singleton

Delegate

MVC
Patterns (Singleton)

Used to ensure that there's only one instance of
a class in an application

Every application has exactly one instance of
UIApplication
UIApplication

The heart of an application:

Sets up the application

Handles events and forwards them

Can get the instance with the message:
[UIApplication sharedApplication];
Patterns (Delegate)

One class handles general behaviour

Has a delegate that handles application specific
behaviour.
Patterns (Delegate)

Applications has an ”appDelegate”

Called by UIApplication for application specific
setup
-
(
void
)applicationDidFinishLaunching:(
UIApplication
*)application

Adopts the protocol UIApplicationDelegate
Patterns (MVC)

Used a LOT in iPhone development
Application life cycle (pre iOS4)

Set up the application (UIApplication,
appDelegate)

Run loop

Terminates
Application life cycle (pre iOS4)

Startup
-
(
BOOL
)application:(
UIApplication
*)application
didFinishLaunchingWithOptions:(
NSDictionary
*)launchOptions {
-
(
void
)applicationDidBecomeActive:(
UIApplication
*)application {

Termination
-
(
void
)applicationDidEnterBackground:(
UIApplication
*)application;
-
(
void
)applicationWillTerminate:(
UIApplication
*)application
You have about 5 seconds
Application life cycle (iOS 4)

Added multitasking

Applications move to background instead of
terminate
Application life cycle (iOS 4)

States

Not running

Inactive

Active

Background

Suspended
Application life cycle (IOS 4)

Startup:
-
(
BOOL
)application:(
UIApplication
*)application
didFinishLaunchingWithOptions:(
NSDictionary
*)launchOptions;
-
(
void
)applicationDidBecomeActive:(
UIApplication
*)application;

Move to background:
-
(
void
)applicationWillResignActive:(
UIApplication
*)application;
-
(
void
)applicationDidEnterBackground:(
UIApplication
*)application;
Application life cycle (IOS 4)

Move to foreground
-
(
void
)applicationWillEnterForeground:(
UIApplication
*)application;
-
(
void
)applicationDidBecomeActive:(
UIApplication
*)application;

Terminate?
Application life cycle (IOS 4)

Can force it to terminate

UIApplicationExitsOnSuspend to the info.plist
-
(
void
)applicationDidEnterBackground:(
UIApplication
*)application;
-
(
void
)applicationWillTerminate:(
UIApplication
*)application
Moving to the background

Save the state

Release as much memory as you can

Remove sensitive information from the screen
UIView

The V in MVC

Shows content

Handles events and forwards them to the view
controller
UIView

A view has one superview

Can have zero or more subviews

Views retain there subviews
UIWindow

A subclass of UIView

The superview of all views in your application

Typically you have one UIWindow in an
application
UIView

Usually you don't create the view directly

Created through the view controller

Apperance described in the nib file (.xib)

Edited in interface builder (IB)
UIView
[demo]
Graphical components

UILabel

UITextView

UIWebView

UIScrollView

UIImageView

MKMapView
UILabel

Used to display a short piece of text

... but can also be used to display a longer one
UITextView

Longer texts

Content will scroll inside the area

Can be edible
Displaying texts

Neither UILabel or UITextView is good at
displaying formatted text

Different fonts

Different colors

Bullet lists

...
UIWebView

Of course to display content from a website

But also to display formatted text
UIScrollView

A container that allows an area to be scrollable

Or zoomable

Included in some views:

UIWebView

MKMapView

UITextView
Demo
View Controller
View Controller

The 'C' in MVC

The glue of your application

Connects the model and the views

Typically one controller per view
View Controller

Subclass UIViewController

A custom View Controller usually contains:

Reference to the data (Model)

Reference to view objects that the controller
interacts with

Customize behaviour by overwrite methods
View controller

Connect the view and the controller with
IBOutlet
View Controller (life cycle, load)
-
(
void
)loadView;

Loads the view with the information from the NIB
file

Overwrite if you want to create the view
programatically, not covered in this course.
-
(
void
)viewDidLoad;
View Controller (life cycle, unload)

The view will stay in memory untill:

The controller is released
OR

The application runs out of memory
View Controller (life cycle, unload)

If the view controller is released

Release all objects that is retained by the VC

Data objects

Outlets
View Controller (life cycle, unload)

If the application runs out of memory, it will try
to unload views not currently being showed

Overwrite viewDidUnload

Release outlets and set them to nil
View Controller (life cycle)
-
(
void
)viewWillAppear:(
BOOL
)animated;
-
(
void
)viewDidAppear:(
BOOL
)animated;
-
(
void
)viewWillDisappear:(
BOOL
)animated;
-
(
void
)viewDidDisappear:(
BOOL
)animated:
View Controller (obtaining the data)

Set the instance variable
OR

Go through the appDelegate
Interface rotation

Implement:
-
(
BOOL
)shouldAutorotateToInterfaceOrientation:(
UIInterfaceOrientation
)
interfaceOrientation
;

Different orientations:
UIInterfaceOrientationPortrait
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
UIInterfaceOrientationUpsideDown
Sub views

To create a view controller with a nib alloc as
normal but message init method:
-
(
id
)initWithNibName:(
NSString
*)
nibName
bundle:(
NSBundle
*)
nibBundle
;
NSBundle represents a place in the file system, nil = the
main bundle

To add a subview to a view:
-
(
void
)addSubview:(
UIView
*)
view
;
Sub views (cont.)
-
(
void
)removeFromSuperview;
-
(
void
)bringSubviewToFront:(
UIView
*)
view
;
-
(
void
)sendSubviewToBack:(
UIView
*)
view
;