UITextField
の6行の単純なUITableViewController
があります。私は各行をクリックしてキーボードをそれぞれに適用させたいと思っています。何らかの理由を除いて、全てが機能しますが、レスポンダが次のものをUITextField
にするために、各行を2回クリックする必要があります。私はUITextFieldDelegate
の印刷物を入れました。彼らの行動の順序は間違っています。 TextFields
にはそれぞれ0〜5のタグが付けられています。最初に選択すると問題はなく、キーボードが現れ、何か入力します。私のプリントアウトは、次のとおりです。textFieldShouldBeginEditingが呼び出され、textFieldDidBeginEditingが呼び出されない
textFieldShouldBeginEditing tag: 0 textFieldDidBeginEditing tag: 0
その後、私は(キーボードで完了押すことなく)次の行を選択し、プリントアウトはこれです:
textFieldShouldBeginEditing tag: 1 textFieldShouldEndEditing tag: 0 textFieldDidEndEditing tag: 0
はなぜtextFieldDidBeginEditing
呼び出されていませんか?
class EstimateCell: UITableViewCell, UITextFieldDelegate {
@IBOutlet weak var estimateField: UITextField!
// callback to alert when user clicked "Done"
var doneTextFieldInput: ((cell: EstimateCell, newText:String?) -> Void)?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
// self.selectionStyle = .None
estimateField.delegate = self
estimateField.clearsOnBeginEditing = true
// add a done button to the numberpad
addDoneButtonOnNumpad(estimateField)
// add some padding to the right margin
estimateField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 0, 0)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
/**
Only allow 4 digits
*/
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// only allow 4 digits max
guard let text = textField.text else { return true }
let newLength = text.characters.count + string.characters.count - range.length
let isMax4digits = newLength <= 5
return isMax4digits
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
print("textFieldShouldBeginEditing \(textField.tag)")
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
textField.text = ""
print("textFieldDidBeginEditing \(textField.tag)")
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
print("textFieldShouldEndEditing \(textField.tag)")
return true
}
/**
Called after the textfield resigns as the first responder
*/
func textFieldDidEndEditing(textField: UITextField) {
print("textFieldDidEndEditing \(textField.tag)")
doneTextFieldInput?(cell: self, newText: textField.text)
// textField.resignFirstResponder()
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
print("textFieldShouldReturn")
textField.resignFirstResponder()
return true
}
/**
Adds a toolbar with a Done button as an input accessory view to the given textfield. Calls
resignFirstResponder when Done is clicked.
*/
func addDoneButtonOnNumpad(textField: UITextField)
{
let keypadToolbar: UIToolbar = UIToolbar()
// add a done button to the numberpad
keypadToolbar.items=[
UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: textField, action: #selector(UITextField.resignFirstResponder)),
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)
]
keypadToolbar.sizeToFit()
// add a toolbar with a done button above the number pad
textField.inputAccessoryView = keypadToolbar
}
}
awakeFromNib()メソッドから以下のコードを削除してみてください estimateField.clearsOnBeginEditing = true – Harish
残念ながらそれはしませんでした。しかし、試してくれてありがとう!私の唯一の他の考え方は、UIViewControllerに変更し、内部にUITableViewを入れて、そのようにしてみることです。私が最初にそれをしなかった理由は、下のテキストフィールドの1つを選択するときに自動スクロール機能を得ることです。 – Coder1224
クイックレビューと幸運のおかげで@ Coder1224 – Harish