我正在尝试使用全视图大小的单元格创建一个集合视图,该单元格在每个单元格(视图)上都有文本字段。这将是我的应用程序的注册页面。每次我翻转单元格/页面(这是一个完整的视图),我希望单元格中的textview变为活动状态,键盘弹出,键入文本以写入单元格的textview。 一切正常,直到我回到上一页,然后单元格中的文本视图不再变为活动状态。这是因为我的.becomeFirstResponder()返回false,尽管我的情况正确。
这是我的代码
var currentPage: Int {
set(newValue){
if newValue <= 0 {curPg = 0}
else if newValue >= book.count {curPg = book.count - 1}
else {curPg = newValue}
switch newValue {
case 0:
refreshButtonSettings()
nextButton.addTarget(self, action: #selector(nextPage), for: .touchUpInside)
backButton.addTarget(self, action: #selector(cancel), for: .touchUpInside)
backButton.setTitle("Cancel", for: .normal)
personName.becomeFirstResponder()
personName.isUserInteractionEnabled = true
case 1:
defaultButtonSettings()
birthDate.becomeFirstResponder()
case 2:
defaultButtonSettings()
nextButton.isEnabled = false
nextButton.alpha = 0.5
userName.becomeFirstResponder()
case 3:
defaultButtonSettings()
passWord.becomeFirstResponder()
case 4:
defaultButtonSettings()
phoneNumber.becomeFirstResponder()
case book.count-1:
refreshButtonSettings()
email.becomeFirstResponder()
backButton.addTarget(self, action: #selector(previousPage), for: .touchUpInside)
backButton.setTitle("< ", for: .normal)
nextButton.addTarget(self, action: #selector(sendEmailVerification), for: .touchUpInside)
default:
return
}
print(personName.isFirstResponder)
print(birthDate.isFirstResponder)
print(userName.isFirstResponder)
print(passWord.isFirstResponder)
print(phoneNumber.isFirstResponder)
print(email.isFirstResponder)
}
get {
return curPg
}
}
func defaultButtonSettings(){
refreshButtonSettings()
nextButton.addTarget(self, action: #selector(nextPage), for: .touchUpInside)
backButton.addTarget(self, action: #selector(previousPage), for: .touchUpInside)
backButton.setTitle("< ", for: .normal)
}
func refreshButtonSettings(){
nextButton.removeTarget(nil, action: nil, for: .allEvents)
backButton.removeTarget(nil, action: nil, for: .allEvents)
nextButton.isEnabled = true
nextButton.alpha = 1
}