Tuesday, June 24, 2014

iOS Tutorial - Part 12 - View and Controller communication, View Lifecycle

View and Controller communications, View Lifecycle 

 

Video Description 

View and Controller communication

In the Model View Controller design pattern we have communications between View and Controller. If Controller wants to send message to the view, it uses IBOutlet. If View wants to send message to the controller that for example some button pressed or the slider has changed it uses IBAction.

View Lifecycle

A series of methods that are sent to the View Controller when things happened. For example if we want to initialize some parameters before view appears to the user, we should use view lifecycle methods. Using these methods means overriding them. Because we override these methods, the first line of each method we should call the super (super class) and the name of the method. Here are the important view lifecycle methods:

viewDidLoad

The most important view lifecycle method is this method. This is a great place for initialization because it will be called only one time during the whole lifecycle and also all outlets are set. But do not initialize things that are related to geometry because at this point we can't be sure if we are on iPhone 5 size or 4 or iPad ...
- (void)viewDidLoad
{
    [super viewDidLoad]; //Always add this line as the first line of code for this method
    //Do initialization
}

viewWillAppear

This method is called each time that the view appears to the screen so it's not a good place for initialization because it could be called more than one time during the lifecycle. But it's a good place for updating the view.
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //Do updating the view
}

viewWillDisappear

This will be called a moment before view disappear from the screen. This is a good place to save the data to the storage. For example remembering the scroll position or saving what user typed so far, in order to retrieve it once s/he comes back to the view.
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //Save Data
}

Sunday, June 8, 2014

iOS Tutorial - Part 11 - Converter App, IBOutlet, UITextField, UILabel, IBAction

Convert Miles to Feet App


Video Description 

IBOutlet

Identifier used to identify a property so Interface Builder can synchronize the display and connection of outlets with Xcode.
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;

UITextField


is a control that displays editable text and sends an action message to a target object when the user presses the return button. You typically use this class to gather small amounts of text from the user and perform some immediate action, such as a search operation, based on that text. It has a method called "text" that returns the value that user typed.
@property (weak, nonatomic) IBOutlet UITextField *milesField;

NSString *numberThatUserTyped = self.milesField.text;

UILabel

Implements a read-only text view. You can use this class to draw one or multiple lines of static text, such as those you might use to identify other parts of your user interface. It has a method called "text" that we can set the value of the label.
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;
self.resultLabel.text = @"The result";

IBAction

Use this type as the return type of any action methods defined in your project. It returns nothing (void), it just a way that Xcode understands it's target action connection.
- (IBAction)convertPressed {
    
}

Source code

//
//  ViewController.m
//  ReviewSession
//
//  Created by HuxTek on 5/25/14.
//  Copyright (c) 2014 HuxTek. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *milesField;
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;
@end

@implementation ViewController

- (IBAction)convertPressed {
    float miles = [self.milesField.text floatValue];
    // 1 miles = 5280.0 feet
    float feet = miles * 5280.0;
    self.resultLabel.text = [[NSNumber numberWithFloat:feet] stringValue];
}

@end

Download

Download converter App from here