我们假设当前视图控制器中有4个文本字段,1个选择器和1个按钮。我在键盘上添加了两个额外的按钮,如下所示:
(按钮图像不是那么重要)我正在尝试的是: 假设我单击第二个文本字段。然后应用程序向我显示带有这些按钮的键盘。我想使用这些按钮切换文本字段。第一个按钮关注上一个(第一个文本字段),第二个按钮关注下一个(第三个文本字段)。它应该像这样循环进行。
循环的意思是:假设用户单击了第四个文本字段。然后假设用户键盘上的下一个按钮。因此,应用程序必须专注于第一个文本字段。
这是源代码:
导入UIKit 类TestViewController:UIViewController,UITextFieldDelegate {
@IBOutlet weak var textField1Outlet: UITextField!
@IBOutlet weak var textField2Outlet: UITextField!
@IBOutlet weak var textField3Outlet: UITextField!
@IBOutlet weak var textField4Outlet: UITextField!
@IBOutlet var contentViewOutlet: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.textField1Outlet.delegate = self
self.textField2Outlet.delegate = self
self.textField3Outlet.delegate = self
self.textField4Outlet.delegate = self
let toolBar = UIToolbar()
toolBar.sizeToFit()
let lastItemButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.rewind, target: self, action: #selector(self.switchToTheLastTextfield))
let nextItemButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fastForward, target: self, action: #selector(self.switchToTheNextTextfield))
toolBar.setItems([lastItemButton, nextItemButton], animated: false)
textField1Outlet.inputAccessoryView = UIView()
textField2Outlet.inputAccessoryView = UIView()
textField3Outlet.inputAccessoryView = UIView()
textField4Outlet.inputAccessoryView = UIView()
textField1Outlet.inputAccessoryView = toolBar
textField2Outlet.inputAccessoryView = toolBar
textField3Outlet.inputAccessoryView = toolBar
textField4Outlet.inputAccessoryView = toolBar
}
@objc func switchToTheLastTextfield(_ textField: UITextField)
{
**// help for this scope please**
}
@objc func switchToTheNextTextfield(_ textField: UITextField)
{
**// help for this scope please**
}
}
我需要编写以下两种方法:switchToTheLastTextfield
和switchToTheNextTextfield
。也许一种方法足以管理聚焦的next和previos文本字段。
我尝试通过下面的此方法实现这些功能,但效果不佳。我尝试过这种方法:
@objc func switchToTheNextTextfield()
{
guard let contentView = self.contentViewOutlet else
{
return
}
for view in contentView.subviews
{
if let tField = view as? UITextField
{
if (tField == textField1Outlet)
{
textField2Outlet.becomeFirstResponder()
}
else if (tField == textField2Outlet)
{
textField3Outlet.becomeFirstResponder()
}
else if (tField == textField3Outlet)
{
textField4Outlet.becomeFirstResponder()
}
else if (tField == textField4Outlet)
{
textField1Outlet.becomeFirstResponder()
}
}
}
}
({Swift 4.2, Xcode 10
)
顺便说一下,这是一个细节:如果下一个项目不是文本字段,则App不会崩溃。此场景也应进行管理。
答案 0 :(得分:1)
这里,主要思想是要有一个包含您所有文本字段的数组,以使分配第一响应者更加清晰。设置完成后,只需设置您的帮助函数 switchToTheNextTextfield 和 switchToTheLastTextfield 。在这些函数中,只需遍历文本字段并查看哪个是当前的第一响应者,然后使数组中的下一个/上一个textField成为第一响应者。查看示例代码和注释。
@IBOutlet weak var textField1Outlet: UITextField!
@IBOutlet weak var textField2Outlet: UITextField!
@IBOutlet weak var textField3Outlet: UITextField!
@IBOutlet weak var textField4Outlet: UITextField!
// have a variable which is an array of the textFields to make it easy to cycle around without a long if condition
lazy var textFields: [UITextField] = [textField1Outlet, textField2Outlet, textField3Outlet, textField4Outlet]
@IBOutlet var contentViewOutlet: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// 1. These can be replaced with below
/*
self.textField1Outlet.delegate = self
self.textField2Outlet.delegate = self
self.textField3Outlet.delegate = self
self.textField4Outlet.delegate = self
*/
// 1. These can replace assigning each of the textFields delegate
textFields.forEach { textField in
textField.delegate = self
}
let toolBar = UIToolbar()
toolBar.sizeToFit()
let lastItemButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.rewind, target: self, action: #selector(self.switchToTheLastTextfield))
let nextItemButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fastForward, target: self, action: #selector(self.switchToTheNextTextfield))
toolBar.setItems([lastItemButton, nextItemButton], animated: false)
// Don't need these since you'll immediate assign toolBar to the textFields
/*
textField1Outlet.inputAccessoryView = UIView()
textField2Outlet.inputAccessoryView = UIView()
textField3Outlet.inputAccessoryView = UIView()
textField4Outlet.inputAccessoryView = UIView()
*/
// These can also be replaced with the code below
/*
textField1Outlet.inputAccessoryView = toolBar
textField2Outlet.inputAccessoryView = toolBar
textField3Outlet.inputAccessoryView = toolBar
textField4Outlet.inputAccessoryView = toolBar
*/
textFields.forEach { textField in
textField.inputAccessoryView = toolBar
}
}
@objc func switchToTheLastTextfield(_ textField: UITextField)
{
guard let contentView = self.contentViewOutlet else
{
return
}
for (index, textField) in textFields.enumerated() {
if textField.isFirstResponder {
var previousIndex = index == 0 ? textFields.count - 1 : index - 1 // ternary to determine the previous index
textFields[previousIndex].becomeFirstResponder()
return
}
}
}
@objc func switchToTheNextTextfield()
{
guard let contentView = self.contentViewOutlet else
{
return
}
for (index, textField) in textFields.enumerated() {
if textField.isFirstResponder {
var nextIndex = index == textFields.count - 1 ? 0 : index + 1 // ternary to determine the next index
textFields[nextIndex].becomeFirstResponder()
return
}
}
}
答案 1 :(得分:0)
一般来说,我不会“手动”浏览您的网点:将所有相关的输入(当前为4个)添加到列表中,并使用一个额外的变量来遍历列表,该变量指示选定的那个。
答案 2 :(得分:0)
尝试以下解决方案:
声明一个UITextfield
变量,并使用textFieldDidBeginEditing
方法分配当前活动的文本字段。
使用按钮操作方法来检查活动的文本字段,并在其中使用becomeFirstResponder
的逻辑。
var currentTextField = UITextField()
func textFieldDidBeginEditing(_ textField: UITextField) {
self.currentTextField = textField
}
@objc func switchToTheNextTextfield()
{
switch self.currentTextField {
case textField1Outlet:
textField2Outlet.becomeFirstResponder()
case textField2Outlet:
textField3Outlet.becomeFirstResponder()
case textField3Outlet:
textField4Outlet.becomeFirstResponder()
case textField4Outlet:
textField1Outlet.becomeFirstResponder()
default:
print("Out of scope")
}
}