View Controllers

Report 6 Downloads 117 Views
INTRODUCTION TO UIKIT The Building Blocks of iOS Apps

WHAT IS UIKIT? Framework that helps us build Apps for iOS Provides default components Provides a skeleton of functionality within which we write our application code Main Components: Views and View Controllers which are used to build Scenes

APPS CONSIST OF SCENES

Login

Main

Settings

AND TRANSITIONS BETWEEN SCENES

Login

Main

Settings

SCENES

SCENES SimpleViewController UITextField

Scenes consist of View Controllers and Views UIView UIView

Views display information and can capture user input (Textfield, Button, ImageView) View Controllers are containers for views

UIButton

UIButton

VIEW CONTROLLERS

VIEW CONTROLLERS In most cases one Screen = one View Controller Each View Controller has one root View Different Views are added as subviews to the View of the View Controller A View Controller has no visual representation! You can only see the View of the View Controller

VIEW CONTROLLERS View Controllers manage Views Get informed about system events: •

View appears on screen



View disappears



Device rotates

VIEWS

VIEWS (Almost) anything that is visible on the iPhone’s screen is a View Views have a size and position, an appearance and can consume user input UIKit provides different View subclasses that represent different components: Image View, Text Fields, etc.

VIEW HIERARCHY

Every View can have other Views as Subviews Views are positioned relative to their parents

HOW DO VIEWS AND VIEW CONTROLLERS WORK TOGETHER?

MODEL VIEW CONTROLLER A View should only display content! A View Controller processes input, modifies/updates the View and communicates with the Model of your application

WHY MVC? Reusability of Views Consistency in Codebase by following convention of MVC Separation of Concerns (general OO principle), makes your codebase easier to understand and extend

VIEW EXAMPLE A UIView receives an object or a set of objects and displays them

UIUserAccountView Benji Developer [email protected] Contact

Delete

accountView.user = currentUser User

View can capture user input but should not perform any complex code, instead hand work off to View Controller

VIEW CONTROLLER EXAMPLE UIUserAccountViewController

UIUserAccountView Benji

ViewWillAppear

Developer [email protected] Download User Data

User Contact

iOS

contactUserPressed

presentEmailViewController

Delete

VIEW + CONTROLLER + MODEL UIUserAccountViewController

UIUserAccountView Benji

ViewWillAppear

Developer User

[email protected]

Download User Data Contact

iOS

contactUserPressed

presentEmailViewController

deleteButtonPressed

removeUser

updateUI

Remove UserAccountView

UserAcounts

removeUserById:

Delete

SUMMARY View and View Controller are the building blocks of iOS Apps View Controllers have no appearance, but they have a root View Every View can have subviews -> View Hierarchy Views should not contain any application logic View Controllers connect Views to the application logic / model

VIEW LIFECYCLE

VIEW LIFECYCLE ViewDidLoad 
 initialize view properties here ViewWillAppear/ViewDidAppear 
 Load data for your view ViewWillDisappear/ViewDidDisappear
 Clean up resources / perform save of edited data

SCENE TRANSITIONS

SCENE TRANSITIONS Transitions between scenes are called Segues Segues can be defined in Storyboard Segues can be triggered by actions or manually in code View Controllers can perform code when segues occur

CONTAINER VIEW CONTROLLERS

CONTAINER VIEW CONTROLLERS Most transitions between Scenes happen within a Container View Controller Most apps have at least one Container View Controller

CONTAINER VIEW CONTROLLERS

UINavigationViewController UITabBarController

SUMMARY Apps consist of multiple Scenes Multiple Scenes are contained in a Container View Controller We transition between Scenes using Segues Scenes consist of View Controllers and Views

ADDITIONAL RESOURCES View Controller Programming Guide https://developer.apple.com/library/ios/featuredarticles/viewcontrollerpgforiphoneos/ View Programming Guide https://developer.apple.com/library/ios/documentation/windowsviews/conceptual/ viewpg_iphoneos/