Why Model-View-Controller?

Similar documents
Designing iphone Applications

View Controllers CPRE 388

Announcements. Today s Topics

Announcements. Today s Topics

Navigation bar (Xcode version 4.5.2) 1. Create a new project. From the Xcode menu, select File > New > Project

User Experience: Windows & Views

View Controller Lifecycle

ios Mobile Development

Naviga&on and Tab Bar Controllers and Table View

Developing Applications for ios

iphone Application Tutorial

src3/bettertextfield/bettertextfield/appdelegate.h // AppDelegate.h // BetterTextField

Your First iphone OS Application

Your First iphone Application

AVAudioPlayer. avtouch Application

My First iphone App (for Xcode version 6.4)

Building GUIs with UIKit. Kevin Cathey

iphone App Basics iphone and ipod touch Development Fall 2009 Lecture 5

Stanford CS193p. Developing Applications for ios. Fall Stanford CS193p. Fall 2011

My First iphone App. 1. Tutorial Overview

MVC and Interface Builder IAP 2010

Apple Development Technology Workshops

ios Core Data Example Application

The Sensors in your iphone. Dr Alasdair Allan

Corrections and version notes

Integrating Game Center into a BuzzTouch 1.5 app

lectures/2/src2/nib1/nib1/appdelegate.h // AppDelegate.h // Nib1 // David J. Malan // Harvard University //

Assignment III: Graphing Calculator

SAMPLE CHAPTER. Brendan G. Lim Martin Conte Mac Donell MANNING

OVERVIEW. Why learn ios programming? Share first-hand experience. Identify platform differences. Identify similarities with.net

The MVC Design Pattern

INTRODUCTION TO ARCHITECTING YOUR IOS APP

User Experience: Windows & Views

IOS - TEXT FIELD. Use of Text Field. Important Properties of Text Field. Updating Properties in xib

iphone Programming Patrick H. Madden SUNY Binghamton Computer Science Department

Building an Application

Widget Tour. iphone and ipod touch Development Fall 2009 Lecture 7

Stanford CS193p. Developing Applications for ios. Fall Stanford CS193p. Fall 2011

Stanford CS193p. Developing Applications for iphone 4, ipod Touch, & ipad Fall Stanford CS193p Fall 2010

ITP 342 Advanced Mobile App Dev. Memory

iphone Programming Touch, Sound, and More! Norman McEntire Founder Servin Flashlight CodeTour TouchCount CodeTour

MVC & Onwards. CS 442: Mobile App Development Michael Saelee

epicurious Anatomy of an ios app Robert Tolar Haining June 25, 2010 ConvergeSE

Life Cycle. Chapter Explore the Game Application. Understanding the Views in a Game

ITP 342 Mobile App Dev. Accelerometer Gyroscope

Create an App that will drop PushPins onto a map based on addresses that the user inputs.

Computer Science 251. iphone Application Development. Autorotation, Popover Controllers, Modal Controllers

CS193P - Lecture 8. iphone Application Development. Scroll Views & Table Views

Structuring an App Copyright 2013 Apple Inc. All Rights Reserved.

ITP 342 Mobile App Development. Model View Controller

Today s Topics. Scroll views Table views. UITableViewController Table view cells. Displaying data Controlling appearance & behavior

Cisco StadiumVision Mobile API for Apple ios

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

ITP 342 Mobile App Dev. Delegates

A Mad Libs app that you will navigate through 3 UIViewControllers to add text that will be shown in a story on the fourth UIViewController.

A Better MVC. 300 line view controllers or bust. Dave A guy who thinks too deeply about stuff

View Controller Advancements for ios8

COPYRIGHTED MATERIAL. part I Developing a Professional UI. Chapter 1: Creating a Personal Library. Chapter 2: Advancing with Tableviews

Praktikum Entwicklung von Mediensystemen mit ios

Assignment III: Graphing Calculator

Views. A view (i.e. UIView subclass) represents a rectangular area Defines a coordinate space

Assignment II: Foundation Calculator

A Vertical Slider for iphone

CSE 438: Mobile Application Development Lab 2: Virtual Pet App

CS193P: HelloPoly Walkthrough

ITP 342 Mobile App Dev. Fundamentals

InterfaceBuilder and user interfaces

iphone Application Programming Lab 2: MVC and Delegation + A01 discussion

AVAudioRecorder & System Sound Services

Implementing UI Designs in Interface Builder

Assignment III: Graphing Calculator

Stanford CS193p. Developing Applications for ios Fall Stanford CS193p. Fall 2013

Saving Data in ios Hands-On Challenges

ios Mobile Development

Cisco StadiumVision Mobile API for Apple ios

Data IAP 2010 iphonedev.csail.mit.edu edward benson / Thursday, January 14, 2010

CS193E Lecture 7. Document-based Applications NSTableView Key-Value Coding

View Concepts. iphone Application Programming Lecture 4: User Interface Design. SDK provide many types of Views to show your content

Stanford CS193p. Developing Applications for ios. Spring CS193p. Spring 2016

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Mobile Apps 2010 iphone and Android

Announcements. Paparazzi 3 assignment is due Wednesday 2/17 This Friday s extra session will feature Evan Doll

My First Cocoa Program

Intro to Native ios Development. Dave Koziol Arbormoon Software, Inc.

Cocoa Programming A Quick-Start Guide for Developers

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

What's new in ios 5?

lecture 10 UI/UX and Programmatic Design cs : spring 2018

Building Mapping Apps for ios With Swift

Mobile Application Programming. Memory Management

CS 47. Beginning iphone Application Development

AUTO LAYOUT IS A UNICORN

Assignment I Walkthrough

ITP 342 Mobile App Development. Model View Controller

CS193P - Lecture 11. iphone Application Development. Text Input Presenting Content Modally

Introductory ios Development

Testing in ios. Paweł Dudek. Thursday, July 4, 13

CS193P - Lecture 11. iphone Application Development. Text Input Presenting Content Modally

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

Mobile Application Programming. Messaging and Delegation

Transcription:

View Controllers

Why Model-View-Controller? Ever used the word spaghetti to describe code? Clear responsibilities make things easier to maintain Avoid having one monster class that does everything

Why Model-View-Controller? Separating responsibilities also leads to reusability By minimizing dependencies, you can take a model or view class you ve already written and use it elsewhere Think of ways to write less code

Communication and MVC How should objects communicate? Which objects know about one another? Model Example: Polygon class Not aware of views or controllers Typically the most reusable Communicate generically using... Key-value observing Notifications

Communication and MVC How should objects communicate? Which objects know about one another? View Example: PolygonView class Not aware of controllers, may be aware of relevant model objects Also tends to be reusable Communicate with controller using... Target-action Delegation

Communication and MVC How should objects communicate? Which objects know about one another? Controller Knows about model and view objects The brains of the operation Manages relationships and data flow Typically app-specific, so rarely reusable

Communication and MVC

View Controllers

Problem: Managing a Screenful Controller manages views, data and application logic Apps are made up of many of these Would be nice to have a well-defined starting point A UIView for views Common language for talking about controllers

Problem: Building Typical Apps Some application flows are very common Navigation-based Tab bar-based Combine the two Don t reinvent the wheel Plug individual screens together to build an app

View Controllers Many of the standard interface behaviors in ios applications; Default behaviors that you can use as is or customize when needed. A convenient way to organize your application s user interface and content.

Organizing Contents in Recipe Management

Patterns for Organizing Content Navigation Bar Tab Bar

Navigation Bar Hierarchy of content Drill down into greater detail

Tab Bar Self-contained modes

Parts of a Screenful

UIViewController Basic building block Manages a screenful of content Subclass to add your application logic

View Controllers In UIKit

Custom View Controllers Create your own UIViewController subclass for each screen Plug them together using existing composite view controllers Every Apps has at least one May interact with other controller objects

Anatomy of View Controller

Key Tasks Customizing View Controller You must configure the view to be loaded by your view controller; You must decide which orientations your view controller supports; You must clean up the memory that is managed by your view controller;

Subclassing UIViewController Member variables pointing to the objects containing the data to be displayed by the corresponding views Member variables (or outlets) pointing to key view objects with which your view controller must interact Action methods that perform tasks associated with buttons and other controls in the view hierarchy Any additional methods needed to implement your view controller s custom behavior

Your View Controller Subclass #import <UIKit/UIKit.h> @interface MyViewController : UIViewController { // A view controller will usually // manage views and data NSMutableArray *mydata; UILabel *mylabel; } // Expose some of its contents to clients @property (readonly) NSArray *mydata; // And respond to actions - (void)dosomeaction:(id)sender;

View Management Cycle (Load)

Unload View from Memory

Creating the View for Your View Controller The main job of a view controller is to load and unload its view as needed. Most view controllers load their views from an associated nib file. MyViewController* vc = [[[MyViewController alloc] initwithnibname:@"myviewcontroller" bundle:nil] autorelease];

Load View from Nib File

@interface MyViewController : UIViewController { } id mybutton; id mytextfield; @property (nonatomic) IBOutlet id mybutton; @property (nonatomic) IBOutlet id mytextfield; - (IBAction)myAction:(id)sender;

Creating views programmatically - (void)loadview { self.wantsfullscreenlayout = YES; MetronomeView *view = [[MetronomeView alloc] @initwithframe:[uiscreen mainscreen].applicationframe]; view.metronomeviewcontroller = self; self.view = view; self.metronomeview = view; [view release]; }

Presenting a View Controller s View Display the view directly by adding it to a window using the addsubview: method. - (void)applicationdidfinishlaunching:(uiapplication *)application { // At this point, the main nib file is loaded. // It is a good idea to set the view's frame before adding it to a window. [viewcontroller.view setframe:[[uiscreen mainscreen] applicationframe]]; } [window addsubview:viewcontroller.view]; [window makekeyandvisible];

Managing Memory Efficiently Allocating critical data structures required by your view controller Creating your view objects Allocating or loading data to be displayed in your view Releasing references to view objects Releasing data that is not needed when your view is not displayed Responding to low-memory notifications Releasing critical data structures required by your view controller Initialization methods loadview viewdidload viewdidunload dealloc viewdidunload didreceivememorywa rning dealloc

Managing a View Controller s Interface Orientation Override your view controller s shouldautorotatetointerfaceorientation: method and declare the orientations it supports; Configure the autoresizing mask for each view in your view controller s view hierarchy;

Processing a one-step interface rotation

Implementing shouldautorotatetointerfaceorientation: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) orientation{ if ((orientation == UIInterfaceOrientationPortrait) (orientation == UIInterfaceOrientationLandscapeLeft)) } return YES; return NO; Four Defined Orientations: UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight

Presenting the landscape view controller @implementation PortraitViewController - (id)init { self = [super initwithnibname:@"portraitview" bundle:nil]; if (self) { isshowinglandscapeview = NO; self.landscapeviewcontroller = [[[LandscapeViewController alloc] initwithnibname:@"landscapeview" bundle:nil] autorelease]; [[UIDevice currentdevice] begingeneratingdeviceorientationnotifications]; [[NSNotificationCenter defaultcenter] addobserver:self selector:@selector(orientationchanged: name:uideviceorientationdidchangenotification object:nil]; } return self; }

- (void)orientationchanged:(nsnotification *)notification { UIDeviceOrientation deviceorientation = [UIDevice currentdevice].orientation; } if (UIDeviceOrientationIsLandscape(deviceOrientation) &&!isshowinglandscapeview) { [self presentmodalviewcontroller:self.landscapeviewcontroller animated:yes]; isshowinglandscapeview = YES; } else if (UIDeviceOrientationIsPortrait(deviceOrientation) && isshowinglandscapeview) { [self dismissmodalviewcontrolleranimated:yes]; isshowinglandscapeview = NO; }

Responding to Orientation Changes in Two Steps