Beliebte Suchanfragen

Cloud Native

DevOps

IT-Security

Agile Methoden

Java

//

Short Introduction to iOS for Java Developers: MVC

27.4.2011 | 2 minutes of reading time

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

1#import <UIKit/UIKit.h>
2 
3@interface WeaponControllsViewController : UIViewController {
4    UISlider *powerSlider;
5}
6 
7@property(nonatomic, retain) IBOutlet UISlider *powerSlider;
8 
9- (IBAction)fire:(id)sender;
10 
11@end

WeaponControllsViewController.m

1#import "WeaponControllsViewController.h"
2 
3@implementation WeaponControllsViewController
4 
5@synthesize powerSlider;
6 
7- (void)viewDidLoad {
8        [super viewDidLoad];
9    self.powerSlider.value = 0;
10}
11 
12- (void)viewDidUnload {
13    self.powerSlider = nil;
14        [super viewDidUnload];
15}
16 
17- (void)dealloc {
18    [powerSlider release], powerSlider = nil;
19        [super dealloc];
20}
21 
22- (IBAction)fire:(id)sender {
23    //...
24}
25 
26@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.

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


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

1- (void)viewDidLoad {
2        [super viewDidLoad];
3    self.powerSlider.value = 0;
4}
5 
6- (void)viewDidUnload {
7    self.powerSlider = nil;
8        [super viewDidUnload];
9}

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):

1WeaponControllsViewController *controller = [[WeaponControllsViewController alloc] initWithNibName:@"WeaponControllsViewController" bundle:nil];
2[controller setTitle:@"Weapons Controll"];
3[self.navigationController pushViewController:controller animated:YES];
4[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

share post

Likes

0

//

More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.