Clear responsibilities make things easier to maintain. Avoid having one monster class that does everything.  Separating responsibilities also leads to reusability by minimizing dependencies, you can take a model or view class you have already written and use it elsewhere. You can think of ways to write less code. How should objects communicate? Which objects know about one another?


  • Example: Polygon class
  • Not aware of views or controllers
  • Typically the most reusable
  • Communicate generically using key-value observing and Notifications Model


  • 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 and Delegation


  • Knows about model and view objects
  • The brains of the operation
  • Manages relationships and data flow
  • Typically app-specific, so rarely reusable

View Controllers

Controller manages views, data and application logic. Many of the applications are made up of these. It would be nice to have a well-defined starting point. A built-in UIView class for using views that provides a common language for talking about controllers.

Some application flows are very common:

  • Navigation-based
  • Tab bar-based
  • Combination of the two

So do not reinvent the wheel. Just plug individual screens together to build an app. These provides basic building block manages a screenful of content and subclass to add your application logic.


Basic building block for creating ViewController. Manages a screenful of content. It provides a subclass to add your application logic.

Create your own UIViewController subclass for each screenful. Plug them together using existing composite view controllers.

ViewController 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 in ViewController

  • UIViewController superclass has a view property
    • @property (retain) UIView *view;
  • Loads lazily
    • On demand when requested
    • Can be purged on demand as well (low memory)
  • Sizing and positioning the view?
    • Depends on where it’s being used
    • Don’t make assumptions, be flexible

Cocoa, the graphical framework of iphone, tends to embrace a lazy philosophy. Calls -release instead of -dealloc. Calls -setNeedsDisplay instead of -drawRect:. Allows work to be deferred or coalesced. Result is the performance increase!

Creating View with Interface Builder

Never call Override -loadView directly! Create your views. Set the view property and create view controller with -init.

// Subclass of UIViewController
- (void)loadView
MyView *myView = [[MyView alloc] initWithFrame:frame];
[myView release];
self.view = myView

Lay out a view in Interface Builder. File’s owner is view controller class.. Hook up view outlet. Create view controller with -initWithNibName:bundle:

Creating ViewController with Interface Builder

- (id)initWithNibName:(NSString *)nibName
bundle:(NSBundle *)bundle
if (self == [super init...]) {
// Perform initial setup, nothing view-related
myData = [[NSMutableArray alloc] init];
self.title = @“Foo”;
return self;
- (void)viewDidLoad
// Your view has been loaded
// Customize it here if needed
view.someWeirdProperty = YES;
- (void)viewWillAppear:(BOOL)animated
[super viewWillAppear:animated];
// Your view is about to show on the screen
[self beginLoadingDataFromTheWeb];
[self startShowingLoadingProgress];
- (void)viewWillDisappear:(BOOL)animated
[super viewWillDisappear:animated];
// Your view is about to leave the screen
[self rememberScrollPosition];
[self saveDataToDisk];

Supporting Interface Rotation

Portrait View

- (BOOL)shouldAutorotateToInterfaceOrientation:
// This view controller only supports portrait
return (interfaceOrientation ==

All Orientation except upside-down

- (BOOL)shouldAutorotateToInterfaceOrientation:
// This view controller supports all orientations
// except for upside-down.
return (interfaceOrientation !=

Loading and Saving Data

Lots of options out there, depends on what you need

  • NSUserDefaults
  • Property lists
  • CoreData
  • SQLite
  • Web services
Tagged with: C/C++ languageiPhone

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.