Source Freeze

  • Home
  • iOS
  • Swift
  • Cross-Platform
  • About
  • Contact

UITextField and UITextField Delegate in swift

May 16, 2015 by Source Freeze 2 Comments

UITextField object is the one of the most commonly used UI Control in the iOS, it displays editable text and sends an action message to a target object, it usually gets a small amount of text from user and perform some immediate action such as search operation, based on that text.

In this tutorial i’m going to explain about UITextField and UITextField delegates, this example written in swift so you must need Xcode 6 or greater version to run this application.

Open the Xcode and create a new single view application.

Then enter the product name as “UITextField Tutorial” next fill out the Organization Name and Organization identifier then select “Swift” language and device family as Universal and  remains core data field as unchecked because we are not using that feature.

UITextField Create Application

UITextField Design Part

Now open the Main.storyboard file, drag and drop the UITextField, UIButton and UILabel, change the UIButton name as “Enter”. The Storyboard will looks like below.

uitextfiled in the storyboard

Next, Select the Assistant Editor to create the IBOutlet for UITextField and UILabel name it as “textField”, “enteredValue” respectively.

IBOutLet for UITextField

Once created above outlet connection the below code added into ViewController.swift file.

viewcontroller file outlet connection code

Then, we have to create a IBAction for UIButton and name it as “buttonClicked” once the connection created, it will add buttonClicked(sender: AnyObject) method into the ViewController.swift file. 

UITextField Enter Button IBAction

Dismiss Keyboard in UITextField

Now run the application you able to type the text in the textFiled, But you not able to dismiss the keyboard while pressing the return key, for dismiss the keyboard we have use resignFirstResponder() and for showing the keyboard we have used becomeFirstResponder(). Here we have handle the hide keyboard and assign the uitextfield text value into the uilabel text in the enter button click by using the below code.

    @IBAction func buttonClicked(sender: AnyObject) {
        textField.resignFirstResponder();
        enteredValue.text = textField.text;
    }

UITextField Delegate

UITextField supports the use of delegate object, it is used to send messages about edit status when certain action occurs. now i am going to explain uitextfield delegate methods in detail.

textFieldShouldBeginEditing : this method gets called just before textfield gets active.

textFieldDidBeginEditing : this method gets called when the textfield active.

textFieldShouldEndEditing :this method gets called before the textfield becomes inactive.

textFieldDidEndEditing : this method gets called when the textfield becomes inactive.

textFieldShouldClear : this method gets called when the clear button pressed.

textFieldshouldChangeCharactersInRange : this method gets called when while typing every single character before its displayed. if you validate the keyboard for editing certain characters this is the best place to implement.

textFieldShouldReturn : this method gets called when return key pressed in the keyboard, and it best place to add the dismiss keyboard function.

Open ViewController.swift file and modify the file like below for adopting uitextfield delegate,

import UIKit
class ViewController: UIViewController, UITextFieldDelegate
{
    @IBOutlet weak var textField: UITextField!
    
    @IBOutlet weak var enteredValue: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        textField.delegate = self;
        // 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.
    }
    @IBAction func buttonClicked(sender: AnyObject) {
        textField.resignFirstResponder();
        enteredValue.text = textField.text;
    }
}

Now, add the textfield delegates methods into the ViewController.swift file, and each delegate method gets called i have printed corresponding methods information  in the console,

// UITextField Delegates
 func textFieldDidBeginEditing(textField: UITextField) {
 println("TextField did begin editing method called")
 }
 func textFieldDidEndEditing(textField: UITextField) {
 println("TextField did end editing method called")
 }
 func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
 println("TextField should begin editing method called")
 return true;
 }
 func textFieldShouldClear(textField: UITextField) -> Bool {
 println("TextField should clear method called")
 return true;
 }
 func textFieldShouldEndEditing(textField: UITextField) -> Bool {
 println("TextField should snd editing method called")
 return true;
 }
 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
 println("While entering the characters this method gets called")
 return true;
 }
 func textFieldShouldReturn(textField: UITextField) -> Bool {
 println("TextField should return method called")
 textField.resignFirstResponder();
 return true;
 }

Then Build and run the application the output will looks like below and Download the UITextField and UITextField Delegate tutorial in this link.

UiTextField and UITextField Delegate Output

 

Filed Under: ios-tutorial, swift, uitextfield

Recent Posts

  • Ionic 2 getting started
  • Best Mobile UI Frameworks using HTML5, CSS and JS
  • MBProgressHUD example in Swift
  • UIActivityindicatorview example in ios using swift
  • ionic side menu example
  • Cordova InAppBrowser Plugin Example using ionic framework

TAGS

cross-platform ionic iOS mobile application development objective-c swift uiwebview Visual Studio plugin

Categories

  • cordova
  • cross-platform
  • Hybrid
  • Ionic 2
  • ionic framework
  • ios-tutorial
  • Mobile App Development
  • phonegap
  • swift
  • UIAlertController
  • uidatepicker
  • uipickerview
  • uislider
  • UISwitch
  • uitableview
  • uitextfield
  • uiwebview
  • visual studio – plugin

Recent Posts

  • Ionic 2 getting started
  • Best Mobile UI Frameworks using HTML5, CSS and JS
  • MBProgressHUD example in Swift
  • UIActivityindicatorview example in ios using swift
  • ionic side menu example
  • Cordova InAppBrowser Plugin Example using ionic framework

Recent Comments

  • zulfi on iOS UIPickerView Example using Swift
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework
  • SourceFreeze on Cordova InAppBrowser Plugin Example using ionic framework
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework
  • SourceFreeze on Cordova InAppBrowser Plugin Example using ionic framework
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework

Tags

cross-platform ionic iOS mobile application development objective-c swift uiwebview Visual Studio plugin

Copyright © 2022 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in