如何从父视图控制器检测容器视图中的焦点 UITextField

How to detect focused UITextField in Container view from Parent View Controller

我有一个容器视图,上面有多个文本框。我在父视图控制器(自定义键盘)中也有一个按钮。我想要做的是首先 select 文本框,当我点击按钮时,我希望将一些值填充到最后一个 selected/focused 文本框。 我怎样才能做到这一点?也欢迎任何其他方式。 (我在原始代码中有多个容器视图,并尝试对所有视图使用一个键盘)

class MainViewController: UIViewController {
    var weightVC : WeightViewController!
    var focusedElement : UITextField

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if (segue.identifier == "weight") {
            weightVC = segue.destination as? WeightViewController
        }
    }

    @IBAction func button1Clicked(_ sender: Any) {

        if weightVC != nil {
            weightVC.sampleTextBox1.text = "1"
            //I want this sampleTextBox1 to be dynamic like weightVC.focusedInput = "1"
        }

    }
}

extension MainViewController:ChildToParentProtocol {
   func setFocusedElement(with value: UITextField){
      focusedElement = value
   }
}

容器视图控制器


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

class WeightViewController: UIViewController {

    weak var delegate:  ChildToParentProtocol? = nil

    @IBOutlet weak var sampleTextBox1: UITextField!
    @IBOutlet weak var sampleTextBox2: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // sampleTextBox1 Editing Did Begin event
    @IBAction func editBeginSampleText1(_ sender: Any) {
        print("edit begin")
        delegate?.setFocusedElement(with: sampleTextBox1)
    }

}

换句话说,我只是想在点击按钮时保留对最后聚焦的 UITextFild 的引用。希望我的要求足够清楚。如果有办法实现这一目标,请指导我。

谢谢

如果我正确理解了您的问题,您可以使用它的标签来跟踪点击了哪个 UITextField。您可以使用 UITextFieldDelegate 获取选定的 UITextField 标签。

考虑以下 WeightViewController

的代码
protocol ChildToParentProtocol: class {
    //Changed value to Int for passing the tag.
    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()

        //Assign delegate and tags to your TF
        tf1.delegate = self
        tf2.delegate = self

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

extension WeightViewController: UITextFieldDelegate {
    func textFieldDidBeginEditing(_ textField: UITextField) {
        //Get the selected TF tag
        selectedTFTag = textField.tag
        //Pass tag to parent view
        delegate?.setFocusedElement(with: selectedTFTag)
    }
}

现在在您的父视图中ViewController您需要进行一些修改。我在为满足您的要求而进行更改的地方添加了评论。

import UIKit

//You need to confirm your ChildToParentProtocol with your UIViewController
class ViewController: UIViewController, ChildToParentProtocol {

    var selectedTFTag = 0
    var weightVC : WeightViewController!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "weight" {
            weightVC = segue.destination as? WeightViewController
            //You need to pass delegate to containerview to make it working.
            weightVC.delegate = self
        }
    }

    @IBAction func btn1Tapped(_ sender: Any) {

        //According to selected Tag perform your action
        if selectedTFTag > 0 {
            switch selectedTFTag {
            case 1:
                //set up first UITextField
                weightVC.tf1.text = "First textfield was selected"
                print("1")
            case 2:
                //set up second UITextField
                weightVC.tf2.text = "Second textfield was selected"
            default:
                break
            }
        }
    }

    @IBAction func btn2Tapped(_ sender: Any) {

        //According to selected Tag perform your action
        if selectedTFTag > 0 {
            switch selectedTFTag {
            case 1:
                //set up first UITextField
                weightVC.tf1.text = "First textfield was selected"
                print("1")
            case 2:
                //set up second UITextField
                weightVC.tf2.text = "Second textfield was selected"
            default:
                break
            }
        }
    }

    func setFocusedElement(with value: Int) {
        //Get selected TF tag with delegate
        selectedTFTag = value
    }
}

您可以查看 THIS 演示项目了解更多信息。