How to Determine Which Textfield Is Active Swift

How to determine which textfield is active swift

You can declare a UITextField property in your class, and assign the current text field to it in textFieldDidBeginEditing.
Then you can just call this text field whenever you need to.

class ViewController : UIViewController, UITextFieldDelegate {

var activeTextField = UITextField()

// Assign the newly active text field to your activeTextField variable
func textFieldDidBeginEditing(textField: UITextField) {

self.activeTextField = textField
}

// Call activeTextField whenever you need to
func anotherMethod() {

// self.activeTextField.text is an optional, we safely unwrap it here
if let activeTextFieldText = self.activeTextField.text {
print("Active text field's text: \(activeTextFieldText)")
return;
}

print("Active text field is empty")
}
}

How to determine the active textfield

Here you go:

var responder = window.firstResponder
if responder.isKind(of: NSText.self) {
let fieldEditor = responder as! NSText
responder = fieldEditor.delegate as! NSResponder
}

At the end is responder the focused control. If the focused control is a NSTextField then the first responder is the field editor, a NSTextView inside the text field. The delegate of the field editor is the control.

Trying to find which text field is active ios

You need to search for an object that has become a first responder. First responder object is the one using the keyboard (actually, it is he one having focus for user input). To check which text field uses the keyboard, iterate over your text fields (or just over all subviews) and use the isFirstResponder method.

EDIT:
As requested, a sample code, assuming all text fields are a subview of the view controller's view:

for (UIView *view in self.view.subviews) {
if (view.isFirstResponder) {
[self doSomethingCleverWithView:view];
}
}

How to detect when a TextField becomes active in SwiftUI?

The answer is to initialize TextField with the onEditingChanged parameter.

We can then execute a closure conditionally depending upon whether the text field was edited or changes were committed:

TextField("", text: $email, onEditingChanged: { changed in
if changed {
// User began editing the text field
}
else {
// User tapped the return key
}
})

Check if UITextfield is active

You could try something like this.

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var passwordTextField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()
self.eyeOpenButton.isHidden = true
passwordTextField.delegate = self
}

// calls when textfield becomes 1st responder
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
switch textField {
case passwordTextField:

if passwordTextField.text != "" {
self.eyeOpenButton.isHidden = !self.eyeOpenButton.isHidden
} else {
self.eyeOpenButton.isHidden = !self.eyeOpenButton.isHidden
}

break
default:
break
}
return true
}

//Calls when the text fields resigns first responder
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {

if textField == passwordTextField {
if passwordTextField.text != "" {
self.eyeOpenButton.isHidden = !self.eyeOpenButton.isHidden
} else {
self.eyeOpenButton.isHidden = !self.eyeOpenButton.isHidden
}
}
return true
}

// Check all the input user has with the keyboard.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let passwordTextField = passwordTextField {

if passwordTextField.text?.count ?? 0 > 0 {
self.eyeOpenButton.isHidden = !self.eyeOpenButton.isHidden
}

if passwordTextField.text?.count == 0 {
self.eyeOpenButton.isHidden = !self.eyeOpenButton.isHidden
}
}

return true
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
for textField in self.view.subviews where textField is UITextField {
textField.resignFirstResponder()
}
return true
}
}

Detect when textfield has been pressed in swift

This function is the callback from UITextFieldDelegate. But it will only be fired if the class with this is connected to your UITextField's delegate.

simple Example using an iOS ViewController:

class yourViewController: UIViewController, UITextFieldDelegate
{
/* Make sure that your variable 'myTextField' was created using an IBOutlet from your storyboard*/
@IBOutlet var myTextField : UITextField!

override func ViewDidLoad()
{
super.viewDidLoad()
myTextField.delegate = self // here you set the delegate so that UITextFieldDelegate's callbacks like textFieldDidBeginEditing respond to events
}

func textFieldDidBeginEditing(_ textField: UITextField) {

if textField == myTextField {
print("pressed")
}
}

}

Make sure though that you understand the concept of the delegation pattern event handling, and how a delegate captures and posts events like this one for instance. A lot of Cocoa GUI components use this design. these are some useful links.

https://docs.swift.org/swift-book/LanguageGuide/Protocols.html

https://developer.apple.com/documentation/uikit/uitextfielddelegate

http://www.andrewcbancroft.com/2015/03/26/what-is-delegation-a-swift-developers-guide/

How to detect when a TextField loses the focus in SwiftUI for iOS?

You can use the initializer init(_:text:onEditingChanged:onCommit:) present in textfield. There you will be getting an action triggered when you begin editing and end editing. You can find out the minimal example below.

import SwiftUI

struct ContentView: View {
@State private var greeting: String = "Hello world!"
var body: some View {
TextField("Welcome", text: $greeting, onEditingChanged: { (editingChanged) in
if editingChanged {
print("TextField focused")
} else {
print("TextField focus removed")
}
})
}
}

Hope this helps.

How to detect live changes on TextField in SwiftUI?

You can create a binding with a custom closure, like this:

struct ContentView: View {
@State var location: String = ""

var body: some View {
let binding = Binding<String>(get: {
self.location
}, set: {
self.location = $0
// do whatever you want here
})

return VStack {
Text("Current location: \(location)")
TextField("Search Location", text: binding)
}

}
}


Related Topics



Leave a reply



Submit