我在2个不同的VC上有2个文本字段。
预期的行为是使文本字段在页面加载时成为第一响应者(无需点击即可自动加载键盘)。
当用户在小键盘上按“完成”时,应辞职响应者并关闭小键盘。
由于某种原因,当我添加代码以允许“完成”按钮退出第一响应者时,似乎阻止了页面加载时该字段成为第一响应者。
在VC1上,我有:
//set the text field as first responder to auto open keyboard
override func viewDidAppear(_ animated: Bool) {
authorNameOutlet.becomeFirstResponder()
}
然后
//make the done button on the keypad dismiss the keyboard
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
authorNameOutlet.resignFirstResponder()
return true
}
此页面不会自动打开键盘并在页面加载时设置第一响应者。
在我的第二个VC上,我删除了textFieldShouldReturn方法,并且第一个响应程序运行良好,通过点击自动打开文本字段上的键盘。但是,在这种情况下,“完成”按钮现在什么也不做,因为“ textFieldShouldReturn”不存在。
我看不到我在做什么错,我以正确的方式将第一个响应者设置在正确的位置(viewDidAppear),并且按下响应者仅应在完成后辞职?
我也有一些验证,但是认为不相关:
//disable button if not all fields completed when finished editing
func textFieldDidEndEditing(_ textField: UITextField) {
if titleOutlet.text?.isEmpty == false {
confirmButtonOutlet.isEnabled = true
} else {
confirmButtonOutlet.isEnabled = false
}
}
我看过其他答案,但这是一个非常简单的实现,无法弄清为什么它不起作用!
更新,所以我只是注意到页面的转换略有不同,并且想起了VC1(它不起作用的地方)是PageView Controller的一部分,而它正在起作用的页面却不是(它是刚从那里隔离了,但是正常的VC)。
我还可以确认将textFieldShouldReturn添加到第二个VC并没有什么不同,所以这是一个红色的鲱鱼。
加入PVC是否有任何理由会改变行为?想知道在该VC中是否有其他代表可以订阅吗?
更新 VC1的整个代码不起作用(注意。我曾尝试删除页面淡入淡出,但这并没有影响它,而第二个VC则相同正在工作)
import UIKit
import RealmSwift
class OnboardingNameVC: UIViewController, UITextFieldDelegate {
@IBOutlet weak var authorNameOutlet: UITextField!
@IBOutlet weak var titleOutlet: UILabel!
@IBOutlet weak var confirmButtonOutlet: UIButton!
@IBAction func confirmNamePressed(_ sender: Any) {
addAuthorName()
}
override func viewDidLoad() {
super.viewDidLoad()
authorNameOutlet.attributedPlaceholder = NSAttributedString(string: "Joe Bloggs",
attributes: [NSAttributedStringKey.foregroundColor: UIColor.lightGray.withAlphaComponent(0.5)])
authorNameOutlet.delegate = self
confirmButtonOutlet.isEnabled = false
authorNameOutlet.setBottomBorder(withColor: UIColor.lightGray)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
authorNameOutlet.becomeFirstResponder()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
animateFade()
}
func addAuthorName() {
let realm = try! Realm()
let authorToCreate = AuthorPreferences()
authorToCreate.AuthorName = authorNameOutlet.text!
try! realm.write {
realm.add(authorToCreate)
performSegue(withIdentifier: "nameToTitleSegue", sender: self)
}
}
//disable button if not all fields completed when finished editing
func textFieldDidEndEditing(_ textField: UITextField) {
if authorNameOutlet.text?.isEmpty == false {
confirmButtonOutlet.isEnabled = true
} else {
confirmButtonOutlet.isEnabled = false
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
authorNameOutlet.resignFirstResponder()
return true
}
//animations
func animateFade() {
let labels : [UIView] = [titleOutlet, authorNameOutlet, confirmButtonOutlet]
var delay = 0.3
let duration = 1.2
for label in labels {
label.fadeIn(duration: duration, delay: delay)
delay += 0.2 // Increment delay
}
}
}
编辑-已解决!
认识到不同的页面转换行为后,我意识到VC1是PageViewController的一部分。
因此,我需要将其添加到viewDidAppear中:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.async {
self.authorNameOutlet.becomeFirstResponder()
}
}
现在可以正常工作了。
TLDR::如果您在pageviewController中具有textField并成为第一响应者无法正常工作,则可能需要按照上述说明将其添加到调度队列中!