Here we will create a simple iPhone Application “Text View," using Xcode Build with pure Swift language and also discuss the keypad issues in IPhone. It is a very simple Application for new developers.
Introduction
In this article, we will create an IPhone Application using Xcode IDE and I will try to make this article interesting by helping the junior and new developer. I start with a very basic issue which is faced in IPhone app development the first time we create a simple Application. We will create a simple Application in which we can see that, when we type any string in the text field, the keypad is slow for typing. When we type and try to hide or return the keypad, it can’t move back so today we will solve this issue also.
Background
Thanks to Xcode IDE, we can create IPhone Application using pure Swift language easily. You can also read more about Swift language at the official Website.
We can test the Application in our simulator and code the testing in Xcode IDE. After this, we can use different methods to resolve the keypad issue.
Using the code
First open the Xcode. I have Xcode version 7.3 and SDK is iOS 9. When Xcode is open, click Create a new project on Xcode. Give the project name “SimpleApp” and select the language Swift. Click next button and set the location where you placed the project. Now, your project is ready.
Main.Storyboard
Click storyboard and go to the toolbox which appears in the left side. Drag and drop the text field, label and one button. Click the label and delete the text. Rename the button with Basic Interaction.
Main.Storyboard To ViewController.swift
Now, click the button and press Ctrl button. Drag into near ViewController.h file, which appears in the left side. After this, it will give an alert message to type the name of this field. When I drag the label field, I give the name SimpleLabel. In the same way, drag and drop the text field and give the name SimpleTextField. For button, give the name Pressbutton.
Run The Application
Now, when you run the Application and type the string in the TextField, you will see what happens when you wish to hide the keypad. You see that the keypad is not hidden. For this problem, we have three methods to hide the keypad.
resignFirstResponsder()
First method is where we add one method, which is known as resignFirstResponsder. When a user clicks the button, the keypad is hidden by doing this method and we implement this code on PressButton event.
- @IBAction func PressButton(sender: AnyObject)
- {
-
- SimpleLabel.text = "Hello " + SimpleTextField.text!
-
- self.SimpleTextField.resignFirstResponder()
- }
touchesbegan() The second method is touchesbegan function, which is a built-in function and we override it with our own way. We add the following code and when a user clicks an empty area, the keypad is hidden.
- override func touchesBegan(touches: Set < UITouch > , withEvent event: UIEvent ? )
- {
- self.view.endEditing(true)
- }
textFieldShouldReturn() The third method is, when we type in textfield, we see that there is a return button in the keypad. When we click the button, the keypad is hidden. By doing this, we add the UITextFieldDelegate in class level, add built-in method textFieldShouldReturn and set return false by doing this, when you run the app and click the return button of the keypad it is hidden.
- class ViewController: UIViewController, UITextFieldDelegate
-
- func textFieldShouldReturn(textField: UITextField) - > Bool
- {
- SimpleTextField.resignFirstResponder()
- return false
- }
Now, I will end the first article. I hope you understood the context and if you face any confusion, feel free to ask your question and I will make more articles on iOS Application development.