gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:45:58 30 4
gpt4 key购买 nike

我有一个容器 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com