iOS UI TextFields

Here we will learn iOS UI text fields in swift with examples and how to use ios UI control text fields in swift to allow users to give input to applications with an example using Xcode editor.

iOS UI Text Fields

In iOS Text fields are used to allow users to enter a single line of text as an input to an app. The text fields automatically bring up a keyboard to enter a text whenever the user touch or taped it and these text fields are helpful to gather small amounts of text from the user like name, email, etc. By using text fields, we can perform some actions like search operation, based on that text.

 

iOS UI TextField Control in Swift Sample Format

 

 We can use Text Fields in our iOS application by adding UITextField class reference in our applications.

 

Now we will see how to use text fields in our iOS applications with example.

Create iOS UI Text Fields App in Xcode

To create a new project in iOS open Xcode from /Applications folder directory. Once we open Xcode the welcome window will open like as shown below. In the welcome window click on the second option “Create a new Xcode Project” or choose File à New à Project.

 

Xcode application to create ios project

 

After selecting “Create a new Xcode project” a new window will open in that we need to choose template.

 

The new Xcode window will contain several built-in app templates to implement common types of iOS apps like page-based apps, tab-based apps, games, table-view apps, etc. These templates are having pre-configured interface and source code files. 

 

For this iOS Text Field example, we will use the most basic template “Single View Application”. To select this one, Go to the iOS section on the left side à select Application à In the main area of dialog select “Single View Application” and then click on the next button like as shown below.

 

Select single view application from ios xcode templates

 

After click Next we will get window like as shown below in this we need to mention the project name and other details for our application.

 

Product Name: “TextField in iOS”

 

The name whatever we enter in the Product Name section will be used for the project and app.

 

Organization Name: “Tutlane”

 

You can enter the name of your organization or your own name or you can leave it as blank.

 

Organization Identifier: “com.developersociety”

 

Enter your organization identifier in case if you don't have any organization identifier enter com.example.

 

Bundle Identifier: This value will generate automatically based on the values we entered in Product Name and Organization Identifier.

 

Language: “Swift”

 

Select language type as “Swift” because we are going to develop applications using swift.

 

Devices: “Universal”

 

Choose Devices options as Universal it means that one application is for all Apple devices in case if you have any specific requirement to run an app only for iPad then you can choose the iPad option to make your application restricted to run only on iPad devices.

 

Use Core Data: Unselected

 

This option is used for database operations. In case if you have any database related operations in your application select this option otherwise unselect the option.

 

Include Unit Tests: Unselected

 

In case if you need unit tests for your application then select this option otherwise unselect it.

 

Include UI Tests: Unselected

 

In case if you need UI tests for your application then select this option otherwise unselect it.

 

Once you finished entering all the options then click on Next button like as shown below

 

Create iOS TextFields App in Swift using Xcode Editor

 

Once we click on the Next button new dialog will open in that we need to select the location to save our project. Once you select the location to save the project then click on Create button like as shown below.

 

Give path to save new ios application in xcode

 

After click on the Create button, the Xcode will create and open a new project. In our project, Main.storyboard and ViewController.swift are the main files that we used to design app user interface and to maintain source code. 

 

Main.storyboard - Its visual interface editor and we will use this file to design our app user interface 

 

ViewController.swift - It contains source code of our application and we use this file to write any code related to our app.

 

Now in project select Main.storyboard file, the Xcode will open visual interface editor like as shown below.

 

ios textfields application storyboard file in xcode

 

Now select ViewController.swift file in your project that view will be like as shown below.

 

ios textfields application viewcontroller file in xcode

Add iOS UI Controls to View

Now we will add controls to our application for that open Object Library. The Object Library will appear at the bottom of Xcode in the right side. In case if you don't find Object library, click on the button which is at the third position from the left in the library selector bar like as shown below. (Alternatively, you can choose View à Utilities à Show Object Library).

 

Object Library in Xcode Application to Search for UI Controls

 

As we discussed our user interface will be in Main.storyboard file so open Main.storyboard file. Now in Object library search for the Text Field in Filter field then drag and drop the Text Field control into Main.storyboard ViewController same way we need to add overall 2 text fields, 3 labels and one-button control to ViewController like as shown below.

 

ios add ui controls to textfields applicaiton in swift

Connect UI Controls to Code in Swift

Now we will make a connection between controls and ViewController.Swift code for that click on the assistant button (overlap circle) in Xcode toolbar right side corner like as shown below

 

Assistant Editor in iOS Xcode to Mapp Controls with Code

 

To map the controls, press the Ctrl button in keyboard and drag the two Text fields, button, and label which we will use to show the text fields text from canvas interface and drop into ViewController.swift file like as shown below 

 

ios map ui controls to viewcontroller code in swift

 

Once we add controls to ViewController.swift file then write the custom code to get text fields text and show it in the label when clicking on the button. Once we write all required functionality our ViewController.swift file code should be like as shown below

 

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var firstname: UITextField!

@IBOutlet weak var lastname: UITextField!

@IBOutlet weak var fullname: UILabel!

var mmm = String()

@IBAction func fullnameAction(sender: AnyObject) {

fullname.text = firstname.text! + lastname.text!

}

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

Now we will run and check the output of the application. To run the application, select the required simulator (Here we selected iPhone 6s Plus) and click on the Play button, located at the top-left corner of the Xcode toolbar like as shown below.

 

Run ios textfields application using simulator

Output of iOS Text Fields Application

Following is the result of the iOS Text Fields application. Now enter text in First Name and Last Name text fields and click on “Full Name button” it will show text fields text in label control like as shown below

 

ios textfiels application example result

 

This is how we can use iOS UI textfield controls in our swift applications to allow users to give input to our applications.