简体   繁体   English

如何在 UITextField 在 Xamarin iOS 中聚焦或失去焦点时检测焦点

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

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.但是,当 UITextfield 聚焦或失去焦点时,不会调用这些事件。

Can some one please help me out with the issue??有人可以帮我解决这个问题吗?

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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM