- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有一个 View -> UITextField 和 UILabel 是两个 child ,我有多个包含 child 的 View 。我正在使用下面的委托(delegate)函数,并将每个 UITextfields 分配为委托(delegate)“textFieldShouldReturn(_ textField: UITextField) -> Bool”。但是,当我按回车键时,它们似乎并没有改变 textField Focus。将此焦点技术与 UITextFields 一起使用,而无需将它们嵌套在 View 中。它使我可以毫无问题地改变焦点。为什么在 View 中嵌套 UITextField 会导致无法让下一个 UITextField 成为第一响应者?我已经阅读了一些关于第一响应者及其工作原理的内容,但它没有清楚地解释如何解决这个问题。
class ScrollingViewWithFields:UIViewController, UITextFieldDelegate {
let scrollView = UIScrollView()
let contentView = UIView()
var textFields:[UITextField] = []
var labeledTextField:[LabeledTextField] = []
override func viewDidLoad() {
contentView.backgroundColor = UIColor.white
contentView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
scrollView.addSubview(contentView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.backgroundColor = UIColor.white
let top = view.safeAreaLayoutGuide.topAnchor
let bottom = view.safeAreaLayoutGuide.bottomAnchor
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: top),
scrollView.bottomAnchor.constraint(equalTo: bottom),
scrollView.leftAnchor.constraint(equalTo: view.leftAnchor),
scrollView.rightAnchor.constraint(equalTo: view.rightAnchor)
])
let ltf = LabeledTextField()
ltf.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(ltf)
ltf.populate(title: "Hello", font: UIFont.systemFont(ofSize: 14.0))
ltf.textField.delegate = self
ltf.textField.tag = 0
let ltf2 = LabeledTextField()
ltf2.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(ltf2)
ltf2.populate(title: "What", font: UIFont.systemFont(ofSize: 14.0))
ltf2.textField.tag = 1
ltf2.textField.delegate = self
self.textFields.append(ltf2.textField)
self.textFields.append(ltf.textField)
NSLayoutConstraint.activate([
ltf.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8.0),
ltf.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8.0),
ltf.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 8.0),
ltf.heightAnchor.constraint(equalToConstant: 60)
])
NSLayoutConstraint.activate([
ltf2.topAnchor.constraint(equalTo: ltf.bottomAnchor, constant: 8.0),
ltf2.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8.0),
ltf2.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 8.0),
ltf2.heightAnchor.constraint(equalToConstant: 60)
])
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
contentView.heightAnchor.constraint(equalToConstant:4000)
])
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let tag = textField.tag
let next = tag + 1
if next < self.textFields.count {
let textField = self.textFields[next]
textField.becomeFirstResponder()
self.scrollView.contentOffset = CGPoint(x: 0.0, y: textField.frame.origin.y - 8.0)
} else {
textField.resignFirstResponder()
}
return true
}
最佳答案
问题在于您在文本字段上设置标签并将它们放入数组的方式。
ltf.textField.tag = 0
ltf2.textField.tag = 1
self.textFields.append(ltf2.textField)
self.textFields.append(ltf.textField)
标签与数组中的顺序不匹配的问题,因为数组最终将成为 [ltf2.textField, ltf.textField]
。我会完全跳过使用标签,而只使用数组中的顺序。
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if let index = textFields.index(of: textField) {
let nextIndex = index + 1
let lastIndex = textFields.count - 1
if nextIndex <= lastIndex {
textFields[nextIndex].becomeFirstResponder()
}
}
return true
}
关于ios - BecomeFirstResponder 和多个 UITextField 焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49391238/
这个问题在这里已经有了答案: Display keyboard without animation (7 个答案) 关闭 9 年前。 只是一个简单的问题,每次我调用 UITextField 的 Be
我目前正在尝试创建一个类似于 Apple Reminders 的待办事项应用。现在,当我通过单击 UIButton 添加新任务时,将创建一个包含 UITextfield 的新单元格并将其指定为第一响应
我有一个 UITextField,用户可以在其中输入他们的名字。当他们点击按钮时,我的应用程序会检查以确保他们确实输入了某些内容。如果他们没有,我会出现一个警告 View ,然后在下面我有这段代码:
我有以下 viewDidLoad 方法: - (void)viewDidLoad { NSLog(@"didLoad"); if (self.loginField.text.lengt
在将其 .hidden 属性设置为 NO 后,我将焦点转移到 UITextView。当我设置 [textView becomeFirstResponder] 时,textView 获得小打字光标,但键
我有一个 View Controller ,其 viewDidLoad 方法在其关联 View 中包含的文本字段(电子邮件)上调用 becomeFirstResponder: -(void) view
我有一个 View 支持复制并使用以下代码显示编辑菜单: if ([self becomeFirstResponder]) { // bring up edit menu. UIMen
我在每个 UITableViewRow 上都有两个 UITextFields,由于某种原因,当我在 UIKeyboard 上按下回车键时,一个特定的行两个 UITextFields 都不会响应(光标不
是否有一种标准方法可以确保键盘出现在屏幕上并且不会消失?我已将我的 textView 设置为第一响应者,以确保键盘显示,但我希望文本出现在 textView 中(让用户知道要输入什么字段),当我成为第
我想将我的 UITextfield 放在底部工具栏中,所以在 ViewWillAppear 上我写了以下代码: textBarcode.BecomeFirstResponder (); 但是 text
我的应用程序的一个目标是管理多个自定义输入 View (来自 nibs)以及常规系统键盘。一个或另一个将永远存在。 (在示例代码中,我只管理一个 Nib + 系统键盘)。 更换键盘时,我想保留您在系统
我在 UITableView 的第一个单元格中有 UITextField,每当它更改时,表格也会更改。问题是,当表更新时,该字段失去第一响应者状态,并且 becomeFirstResponder 不会
在我的应用中,用户可以输入他们的名字。 UITextField 被添加到 View 并调用 becomeFirstResponder。在 textFieldShouldReturn 方法中,调用 re
嗨。我用指纹写了一个代码。当指纹失败时,您可以点击“输入密码”,然后密码 textField 将 becomeFirstResponder()。我的问题在这里:当我点击那个按钮时,它执行 become
我有一个 UIAlertController,在我的应用程序上显示了一个 textField。选择了 textField(标记在其中闪烁)并且在 textField 上设置了 .becomeFirst
我正在使用带有自定义单元格的 UIColletion View ,其中包含 UITextField。正如我发现的那样,当 textField 成为第一响应者时,collectionView 会自动滚动
我有一个包含一个部分和许多行的分组 UITableViewController。每个 cell 包含两个元素:一个带有描述的 UILabel 和一个用于输入的 UITextField。一种形式,我会说
第一次调用 becomeFirstResponder 时,速度很慢。 Instruments 测量大约 160 毫秒......当它完成加载所有它必须加载的东西以便在屏幕上显示键盘时,键盘就会出现!杀
在我的程序中,我需要显示来自文本字段的文本,它在工具栏中不是用户交互的,它是键盘的附属 View 。当我按下按钮时,这部分工作。然后,如果设备旋转,我需要隐藏键盘。这部分也可以正常工作。但是当我回到
我有一个 UIScrollView,其中有一个 UITextField,所以我所做的是在第三个文本字段(密码)上向下滚动 UIScrollView 的偏移量: #pragma mark UITextF
我是一名优秀的程序员,十分优秀!