我有一个容器视图,上面有多个文本框。我在父视图控制器(自定义键盘)中也有一个按钮。我要做的是首先选择文本框,当我点击按钮时,我希望将一些值填充到最后选择/聚焦的文本框。我怎样才能做到这一点?也欢迎任何替代方式。(我在原始代码中有多个容器视图,并尝试对所有视图使用一个键盘)
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 的引用。希望我的要求足够明确。如果有办法实现这一点,请指导我。
谢谢