ios - 如何从父 View Controller 检测容器 View 中的焦点 UITextField

标签 ios swift uitextfield segue container-view

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

storyboard

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

容器 View Controller


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

现在在您的父 View 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演示项目了解更多信息。

关于ios - 如何从父 View Controller 检测容器 View 中的焦点 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55374912/

相关文章:

ios - 在另一个创建 rxswift 中订阅单个 observable

ios - 如何将 iOS 键盘背景更改为更深的颜色,即黑色?

iphone - 如何在 UITextField 中设置光标起始位置

ios - UITextField 文本不在左侧滚动

ios - 在 MacOS 上安装 TensorFlow-experimental 时出错 - curl : (60) SSL certificate

ios - 如何快速将日期、月份和年份转换为毫秒

ios - 在 UITests 中获取 UISwitch 状态/值

ios - AudioServicesPlaySystemSound 奇怪的行为

ios - 如果通过复制粘贴输入文本,UITextView 计算高度错误

json - swift 1.2 中的 SwiftyJson 错误