blog posts

Learn the text of UI fields (user interface) in iOS

Learn about the text of UI fields in iOS and learn to use this text of ui control fields in swift language; This feature allows the user to input applications.

Text UI fields in iOS

In iOS, text fields are used to allow the user to enter a simple line of text as application input. Text fields are automatically suitable for receiving small amounts of information from the user, such as name, email, and.. Using text fields; We can do things like perform a text-based search function.

iOS UI TextField Control in Swift Sample Format

By adding the UITextField class reference to our apps, we can use text fields in our iOS apps.

Now with an example we can see how to use text fields in our applications.

Create UI text fields in iOS apps in Xcode

To create a new project on iOS, open Xcode from the application folder list (applican). After opening Xcode, a welcome window will open as shown below. In the Welcome window, click on the second option create a Xcode Project.

Xcode application to create ios project

After selecting “Create a new Xcode project”, a new window will open in which we must select our template.

The new Xcode window includes several built-in application templates to implement the usual type of iOS apps, such as page-based apps, tab-based apps, games, spreadsheet apps, and more. These templates have a preset interface and source code files. .

For example, text texts in iOS, we have the most basic pattern of the program, which is “demo only”; We will use. To select this item, go to the iOS section on the left, select the application from the application section, and in the main part of the window that opens, select the “single view application” option, and just like the image below, click on Click the next button.

After clicking Next, we will see a window like the one below, in this case we have to mention the project name and other details for our program.

Product name: “Image View in iOS”

The name we enter in the Product Name section is used for the project and application.

Organization name: “Tutlane”

You can enter the name of the organization or your name in this field; Of course you can leave that section blank.

Organization Identifier): “com.developersocociety)”

If you do not have an enterprise ID, enter com.example.

Bundle Identifier

This section is automatically generated based on the phrases we entered in the product name and organization ID.

Language: “Swift”

Select the language type “Swift” because we want to develop applications using swift.

Universal (devices): “Universal”

Select the Devices option as Universal This means that this app is for all Apple devices; If you need to run the app for iPad only, you can select the iPad option to restrict your app to running only on iPad devices.

Use core Data: Not selected

This option is used for database operations. Select this option if you are doing any database-related operations in your application, otherwise do not select this option.

Include Unit Test: Not selected

If you need unit tests for your application, select this option otherwise leave it unselected.

Includes UI tests: not selected

Select this option if you need UI tests for your application, otherwise do not select it.

After completing the options, click the Next button as shown below.

Create ios progress bar application using xcode

When we click the Next button; A new window will open in which you must use the location where you want the new project to be saved; Let’s choose. Once you have selected the new project storage location; As shown below; Click the Create button.

Give path to save new ios application in xcode

After clicking the Create button, Xcode opens and creates a new project. In our project, Main.storyboard and ViewController.swift are the main files used to design the user interface and maintain the source code.

Main.storyboard – which is the visual interface editor and this file is used to design the application interface.

ViewController.swift – which contains the source code of our application and we use this file to write any code related to our application.

Now select the Main.storyboard file in the project, until Xcode opens the visual interface editor as shown below.

ios textfields application storyboard file in xcode

Then select the ViewControl.swift file in your project, which is similar to the following figure.

ios textfields application viewcontroller file in xcode

Add ioS UI controls to the screen in Swift

We are now adding controls to our application for that library of available objects. The object library appears at the bottom right of Xcode. If you did not find the object library, as shown below; Click the button that is the third button in the library selection bar on the left. (Alternatively, you can select the View Utilities Show Object Library path)

As mentioned, our user interface is in the Main.storyboard file, so open the Mian.storyboard file. Now in the objects library in the filter section, look for the Text field of the text field and then drag the text field control and drop it in the Main.storyboard view controller; Similarly, we need to add 2 text fields, 3 labels and a button control to ViewController to get the following.

C: \ Users \ mohammad \ Desktop \ ios-textfields-add-controls-to-view-in-xcode.png

Linking UI controls to coding in Swift

We can now communicate between the controls and the ViewController.Swift code. To do this, click on the Assistant button, which is located on the right side of the Xcode toolbar, as shown below.

To map the controls, hold down the Ctrl key on the keyboard, as shown below; The two text fields, the button and the label that we want to show the text fields; Drag it from the interface and drop it in the ViewController.swift file.

ios map ui controls to viewcontroller code in swift

When we add the control to the ViewController.swift file, we then write the desired code to receive the text of the text fields, and when the button is clicked, that text is displayed in the label. Once we have written all the required functions, our ViewController.swift file code should look like the following:

Import UIkit

Class ViewController: UIViewController {

Var mmm = String ()

@IBAction func var fullnamAction (sender: Any0bject) {

Fullname.text = firstname.text!

}

Override func viewDidLoad () {

Super.viewDidLoad ()

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

}

Override func didrReciveMemoryWaning () {

super. didrReciveMemoryWaning ()

// Dispose of any recources that can be recreated.

}

}

Now run and check the output of the program. To implement the program, you must select the emulator you need (here we have selected the iPhone Six S Plus)

IOS text fields app output

The following is the output of the text fields program. Now enter the text in the First Name and Last name fields; Then click on the “Full Name button” to display the text fields in the label control as shown below.