Sunday, April 3, 2016

Swift Tutorial - Part 4 - Converter App, IBOutlet, UITextField, UILabel, IBAction

Convert Miles to Feet App



Video Description 

IBOutlet

Identifier used to identify a variable so Interface Builder can synchronize the display and connection of outlets with Xcode.
@IBOutlet weak var resultsLabel: UILabel!

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.
    @IBOutlet weak var milesField: UITextField!

var textFieldValue = 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.
@IBOutlet weak var resultsLabel: UILabel!
resultsLabel.text = "Some Value for the label";

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 func convertPressed(sender: UIButton) {
        
    }

Source code

//
//  ViewController.swift
//  SConverterApp
//
//  Created by Hamid on 4/3/16.
//  Copyright © 2016 HuxTek. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var milesField: UITextField!

    @IBOutlet weak var resultsLabel: UILabel!
    
    @IBAction func convertPressed(sender: UIButton) {
        // 1 mile = 5280.0 feet
        let miles = Float(milesField.text!)
        let feets = miles! * 5280.0
        resultsLabel.text = String(feets)
    }
}

Download

Download converter App from here

No comments:

Post a Comment