Short Introduction to iOS for Java Developers: MVC - codecentric AG Blog

:

In iOS, windows and views are used to present the application content. Windows just provide a basic container for the application’s views. During iOS development you’ll notice that Model-View-Controller (MVC) pattern is used extensively. Coming from Java you’ll be familiar with this pattern, and this proves to be very helpful. Although you can build views programmatically, you’ll find out that it’s usually much easier to use Interface Builder for this task. It greatly simplifies the work you have to do in creating your application’s user interface. It enables you to graphically construct and configure your windows and views. The assembled views are place in a nib (.xib) file (a resource file that stores a freeze-dried version of the views). The nib file is loaded at runtime, and objects inside it are reconstituted into actual objects that your can then manipulate programmatically.

View Controllers manage the views and they provide a link between an application’s data and its visual appearance. They are descendants of the UIViewController class, defined in the UIKit framework. View controllers can be divided into three general categories that reflect their role:

  • Custom View Controller: Controller object that you define for the express purpose of presenting your content. You’ll usually have a decent number of these.
  • Container View Controller: Specific type of view controller that manages other view controllers and defines the navigational relationships between them (UINavigationController, UITabBarController, etc). You use the container view controllers provided by the system as is.
  • Modal View Controller: View controller (container or custom) that is presented modally.


Here is an example of custom view controller:

WeaponControllsViewController.h

#import <UIKit/UIKit.h>
 
@interface WeaponControllsViewController : UIViewController {
	UISlider *powerSlider;
}
 
@property(nonatomic, retain) IBOutlet UISlider *powerSlider;
 
- (IBAction)fire:(id)sender;
 
@end

#import <UIKit/UIKit.h>@interface WeaponControllsViewController : UIViewController { UISlider *powerSlider; }@property(nonatomic, retain) IBOutlet UISlider *powerSlider;- (IBAction)fire:(id)sender;@end

WeaponControllsViewController.m

#import "WeaponControllsViewController.h"
 
@implementation WeaponControllsViewController
 
@synthesize powerSlider;
 
- (void)viewDidLoad {
        [super viewDidLoad];
	self.powerSlider.value = 0;
}
 
- (void)viewDidUnload {
	self.powerSlider = nil;
        [super viewDidUnload];
}
 
- (void)dealloc {
	[powerSlider release], powerSlider = nil;
        [super dealloc];
}
 
- (IBAction)fire:(id)sender {
	//...
}
 
@end

#import "WeaponControllsViewController.h"@implementation WeaponControllsViewController@synthesize powerSlider;- (void)viewDidLoad { [super viewDidLoad]; self.powerSlider.value = 0; }- (void)viewDidUnload { self.powerSlider = nil; [super viewDidUnload]; }- (void)dealloc { [powerSlider release], powerSlider = nil; [super dealloc]; }- (IBAction)fire:(id)sender { //... }@end

Outlets and Actions are used to connect your code with the components and their events defined in a nib file. By using IBAction for methods and IBOutlet for properties we are able to connect fire: method to the fire button and powerSlider property to its corresponding component in the view.

@property(nonatomic, retain) IBOutlet UISlider *powerSlider;
 
- (IBAction)fire:(id)sender;

@property(nonatomic, retain) IBOutlet UISlider *powerSlider;- (IBAction)fire:(id)sender;

We use lifecycle methods (viewDidLoad, viewDidUnload, viewDidAppear:, viewDidDisappear:) to prepare and cleanup our view controllers.

- (void)viewDidLoad {
        [super viewDidLoad];
	self.powerSlider.value = 0;
}
 
- (void)viewDidUnload {
	self.powerSlider = nil;
        [super viewDidUnload];
}

- (void)viewDidLoad { [super viewDidLoad]; self.powerSlider.value = 0; }- (void)viewDidUnload { self.powerSlider = nil; [super viewDidUnload]; }

And this is how we would instantiate and present our controller and its views that we defined with Interface Builder (our views where saved in a file named WeaponControllsViewController.xib):

WeaponControllsViewController *controller = [[WeaponControllsViewController alloc] initWithNibName:@"WeaponControllsViewController" bundle:nil];
[controller setTitle:@"Weapons Controll"];
[self.navigationController pushViewController:controller animated:YES];
[controller release];

WeaponControllsViewController *controller = [[WeaponControllsViewController alloc] initWithNibName:@"WeaponControllsViewController" bundle:nil]; [controller setTitle:@"Weapons Controll"]; [self.navigationController pushViewController:controller animated:YES]; [controller release];

You can read more about view controller programming here.
Here is a short tutorial for designing a user interface with Interface Builder.

Previous: Objective-C