• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • About
  • Portfolio
  • Privacy Policy

UITextFieldDelegate

UITextFieldDelegate in Swift

April 7, 2015 By Ravi Shankar Leave a Comment

This is a beginners tutorial on UITextFieldDelegate in Swift. We are going to see how to use UITextFieldDelegate by writing a Simple Interest Calculator.

201504071303.jpg

Download the source code from here

This calculator uses UILabels and TextFields for displaying and accepting amount and interest. We are going to use the UITextFieldDelegate method to navigate from “Principal Amount” to “Rate of Interest”. And when the user taps done on “Rate of Interest” UITextField, the interest is calculated and displayed on the corresponding label.

Source Code

import UIKit

class TextFieldDemoController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var amountTextField: UITextField!

  

@IBOutlet weak var rateTextField: UITextField!

  

@IBOutlet weak var interestLabel: UILabel!

  

var textFields:[UITextField] = []

  

override func viewDidLoad() {

super.viewDidLoad()

  

self.amountTextField.delegate = self

self.rateTextField.delegate = self

  

textFields = [amountTextField, rateTextField]

  

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

  

//MARK :- TextField Delegate

  

func textFieldShouldReturn(textField: UITextField) -> Bool {

  

var currentTextField = textFields[0]

  

if (currentTextField == textField) {

currentTextField = textFields[1]

currentTextField.becomeFirstResponder()

} else {

currentTextField.resignFirstResponder()

interestLabel.text = “\(calculateInterest())“

}

  

return true

}

  

//MARK :- Calculation

  

func calculateInterest() -> Double {

let amount: Double = (amountTextField.text as NSString).doubleValue

let rate:Double = (rateTextField.text as NSString).doubleValue

  

return amount * rate

}

}

Step 1: Make sure your view controller class conforms to UITextFieldDelegate

Step 2: In ViewDidLoad method, set the delegate for the textfields to self (ViewController)
Step 3: Implement textFieldShouldReturn(textField: UITextField) -> Bool(UITextFieldDelegate method) and the code that makes the UITextField FirstResponder and calculates the Interest once the values are entered.
Download the source code from here

Filed Under: Apple, Develop, Programming, Xcode Tagged With: Apple, textFieldShouldReturn, UITextFieldDelegate, Xcode

Primary Sidebar

Recent Posts

  • We have blocked all requests from this device – Firebase Phone Authentication
  • iPhone is not available error message in Xcode
  • Clear CocoaPods cache, re-download and reinstall all pods
  • PDFKit – View, Annotate PDF file in Swift
  • Tab Bar Controller with WebView

Archives

  • September 2020
  • April 2020
  • December 2019
  • November 2019
  • October 2019
  • February 2019
  • October 2017
  • June 2017
  • May 2017
  • March 2017
  • September 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • April 2011
  • March 2011
  • January 2011
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • July 2009
  • March 2008

Copyright 2020 © rshankar.com