0

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

故事板

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 的引用。希望我的要求足够明确。如果有办法实现这一点,请指导我。

谢谢

4

1 回答 1

1

如果我正确理解了您的问题,您可以使用它的标签跟踪哪个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
    }
}

您可以查看演示项目以获取更多信息。

于 2019-03-27T10:56:03.233 回答