iOS/Swift: how to detect touch action on a UITextField

uitextfield tap event
uitextfield get text while typing (swift)
how to check which textfield is active in ios swift
check if textfield is editing ios
get text from uitextfield swift 4
detect change in textfield swift
swift uitextfield end editing
uitextfield editing changed programmatically

I would like to detect the touch action on a UITextField.

It seems the "Touch Up Inside" action is not fired by touching inside the textfield.

It seems "Touch Up Inside" is not enabled for UITextField, but "Touch Down" works.

So the solution is as follows:

Swift 4.x

myTextField.addTarget(self, action: #selector(myTargetFunction), for: .touchDown)
@objc func myTargetFunction(textField: UITextField) {
    print("myTargetFunction")
}

Swift 3.x

myTextField.addTarget(self, action: #selector(myTargetFunction), for: UIControlEvents.touchDown)

@objc func myTargetFunction(textField: UITextField) {
    print("myTargetFunction")
}

iOS/Swift: how to detect touch action on a UITextField, It seems "Touch Up Inside" is not enabled for UITextField , but "Touch Down" works. So the solution is as follows: Swift 4.x myTextField.addTarget(self, action: � You use text fields to gather text-based input from the user using the onscreen keyboard. The keyboard is configurable for many different types of input such as plain text, emails, numbers, and so on. Text fields use the target-action mechanism and a delegate object to report changes made during the course of editing.

Swift 4 - 5:
class ViewController: UIViewController, UITextFieldDelegate {
    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField == myTextField {
            print("You edit myTextField")
        }
    }
}

This is a delegate function.

🤦 🧝 iOS / Swift: how to detect touch action on UITextField, It seems “Touch Up Inside” is not enabled for UITextField , but “Touch Down” works. So the solution is as follows: Swift 4.x myTextField.addTarget(self, action: � Text Input and Delegation via UITextField iOS 10.05.2020. UITextField is part of UIKit framework and is used to display an area to collect text input from the user using the onscreen keyboard. When a text field is tapped, the keyboard automatically slides up onto the screen. (You will see why this happens later in this chapter.)

here's Swfit:

and you don't need to use the "touchUpInside" just use the delegate methods like so:

Make your View controller a delegate:

class ViewController: UIViewController, UITextFieldDelegate{

func textFieldDidBeginEditing(textField: UITextField) -> Bool {
    if textField == myTextField {
        return true // myTextField was touched
    }
}

Here's the other delegate methods:

protocol UITextFieldDelegate : NSObjectProtocol {

    optional func textFieldShouldBeginEditing(textField: UITextField) -> Bool // return NO to disallow editing.
    optional func textFieldDidBeginEditing(textField: UITextField) // became first responder
    optional func textFieldShouldEndEditing(textField: UITextField) -> Bool // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
    optional func textFieldDidEndEditing(textField: UITextField) // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called

    optional func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool // return NO to not change text

    optional func textFieldShouldClear(textField: UITextField) -> Bool // called when clear button pressed. return NO to ignore (no notifications)
    optional func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return NO to ignore.
}

from swift docs:

struct UIControlEvents : RawOptionSetType {
    init(_ rawValue: UInt)
    init(rawValue: UInt)

    static var TouchDown: UIControlEvents { get } // on all touch downs
    static var TouchDownRepeat: UIControlEvents { get } // on multiple touchdowns (tap count > 1)
    static var TouchDragInside: UIControlEvents { get }
    static var TouchDragOutside: UIControlEvents { get }
    static var TouchDragEnter: UIControlEvents { get }
    static var TouchDragExit: UIControlEvents { get }
    static var TouchUpInside: UIControlEvents { get }
    static var TouchUpOutside: UIControlEvents { get }
    static var TouchCancel: UIControlEvents { get }

    static var ValueChanged: UIControlEvents { get } // sliders, etc.

    static var EditingDidBegin: UIControlEvents { get } // UITextField
    static var EditingChanged: UIControlEvents { get }
    static var EditingDidEnd: UIControlEvents { get }
    static var EditingDidEndOnExit: UIControlEvents { get } // 'return key' ending editing

    static var AllTouchEvents: UIControlEvents { get } // for touch events
    static var AllEditingEvents: UIControlEvents { get } // for UITextField
    static var ApplicationReserved: UIControlEvents { get } // range available for application use
    static var SystemReserved: UIControlEvents { get } // range reserved for internal framework use
    static var AllEvents: UIControlEvents { get }
}

UITextField doesn't respond to "touchUpInside" see to the right side, you'll find it's acceptable control events

How to detect tap on UITextField?, textField.frame, point)) { [self doSomething]; } } } Swift 3 func viewDidLoad() { let tapGesture = UITapGestureRecognizer(target: self, action:� In your storyboard, click the assistant editor at the top of the screen (two circles in the middle). Ctrl + Click on the textfield in interface builder. Drag from EditingChanged to inside your view controller class in the assistant view. Name your function ("textDidChange" for example) and click connect.

Update For Swift 3

Here is the code for Swift 3:

myTextField.addTarget(self, action: #selector(myTargetFunction), for: .touchDown)

This is the function:

func myTargetFunction() {
    print("It works!")
}

How to Get UITextField Text Editing Changed Events for User Input , The Target/Action method names have changed in Swift 3, more details down below. Xcode Project. Download the Xcode project from the free� Swift version: 5.2 Paul Hudson @twostraws May 28th 2019 If you’re in a situation where your iOS app has multiple UITextField instances lined up, users expect to be able to move between them by pressing Next/Return on their on-screen keyboard.

I referred to the UIControlEvents documentation from Apple and came up with the following:

First add UITextFieldDelegate to your class then,

textBox.delegate = self
textBox.addTarget(self, action: #selector(TextBoxOn(_:)),for: .editingDidBegin)
textBox.addTarget(self, action: #selector(TextBoxOff(_:)),for: .editingDidEnd)

with the following functions:

func TextBoxOff(_ textField: UITextField) {
     code
         }                
}

func TextBox(_ textField: UITextField) {
    code
         }
}

UIControl.Event, A repeated touch-down event in the control; for this event the value of the A touch initiating an editing session in a UITextField object by entering its bounds. The UITableView is constrained to be the same width as the UITextField, which you can see by looking at the constraints in the Storyboard. If you want the result of your editing and selecting in the UITextField to trigger some other action in your app, you do this in the textFieldDidEndEditing method as also mentioned in the code comments.

Handling Tap Gestures, Tap gestures detect one or more fingers touching the screen briefly. to those of a button—it detects a tap in its view and reports that tap to your action method. Swift version: 5.2 Paul Hudson @twostraws May 28th 2019 The iOS UITapGestureRecognizer class has a built-in way to detect a double tap on any view. All you need to do is create the recognizer, set its numberOfTapsRequired property to 2, then add it to the view you want to monitor.

How to detect a double tap gesture, The iOS UITapGestureRecognizer class has a built-in way to detect a viewDidLoad() let tap = UITapGestureRecognizer(target: self, action:� Note: Seems like while you are in the process of editing a UITextField with the built-in iOS keyboard, the "text" property of the text field is not updated with every new letter typed/removed. Instead, the text field object gets updated "as a whole" after you resign the first responder status of the text field.

How to Dismiss UITextField's Keyboard in your Swift App, Once you tap outside of the UITextField, the keyboard will be dismissed. How does this work? I'll let the Apple Documentation for the endEditing� If you need to take text input in your Swift app, you will probably need a UITextField. It is exactly what it sounds like, just a field on the screen where the user types something in. When the user taps on it, the keyboard comes up from the bottom of the screen, and allows the user to start typing.

Comments
  • Interesting solution, but doesn't work as of Swift 4.
  • Works for me, 4.1 swift
  • i am trying to call webservice on textfield click on keyboard comes and webservice never called. But when i use editingBegin property that time web service called and it goes in infinity loop. Can anyone tell me how can make textfield uneditable,
  • shouldBeginEditing is th best delegate method for this purpose.
  • @Gowthaman you might have forgotten to set the textfield delegate to self (the ViewController)
  • Thanks @Lacerax, but I would like to detect then the user taps the textfield. I am already detecting editing with this function myTextField.addTarget(self, action: "myTargetFunction:", forControlEvents: UIControlEvents.EditingChanged), without the need of a UITextFieldDelegate
  • so you mean touch but not enter the textfield, right?
  • You shoudl read the docs on UITextField, it only responds to these selector methods: UIControlEventEditingDidBegin = 1 << 16, // UITextField UIControlEventEditingChanged = 1 << 17, UIControlEventEditingDidEnd = 1 << 18, UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing
  • touchupinside is NOT one of those
  • look at the answer in one se
  • All answer except your answer was useless for me! Only it worked perfectly! Thanks :)
  • Awesome Answer, Perfectly Worked for me, Thanks :)
  • This doesn't work... I'm getting: Value of type 'UITextView' has no member 'addTarget'