How to detect focus while UITextField is in focus or lost the focus in Xamarin iOS

Viewed 32

I tried subscribing for events to detect focus change with below event handlers.

For Focus

UITextFieldObject.EditingDidBegin += EditingDidBegin;
OR 
UITextFieldObject.Started += EditingStarted;

For lost Focus

UITextFieldObject.EditingDidEnd += EditingDidEnd;
OR
UITextFieldObject.Ended += EditingEnded;

But these events are not invoked while UITextfield is focused or lost focus.

Can some one please help me out with the issue??

1 Answers

If I understood your question correctly you can keep track on which is tapped by using it's tag. And you can use to get the selected.

protocol ChildToParentProtocol: class {
 
    func setFocusedElement(with value: Int)
}

import UIKit

class WeightViewController: UIViewController {

    @IBOutlet weak var tf1: UITextField!
    @IBOutlet weak var tf2: UITextField!

    var selectedTFTag = 0
    weak var delegate: ChildToParentProtocol? = nil

    override func viewDidLoad() {
        super.viewDidLoad()

       
        tf1.delegate = self
        tf2.delegate = self

        tf1.tag = 1
        tf2.tag = 2
    }
}

extension WeightViewController: UITextFieldDelegate {
    func textFieldDidBeginEditing(_ textField: UITextField) {
        
        selectedTFTag = textField.tag
      
        delegate?.setFocusedElement(with: selectedTFTag)
    }
}
Related