我对iOS/Swift还很新,当我将UITextView
设置为第一响应者(从上一个UITextField
触摸next
时)时,它会自动在UITextView
中插入一个新行。 代码如下:
class MyViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var descriptionTextView: UITextView!
override func viewDidLoad() {
nameTextField.delegate = self
descriptionTextView.delegate = self
}
func textFieldShouldReturn(nameTextField: UITextField) -> Bool {
nameTextField.resignFirstResponder()
descriptionTextView.becomeFirstResponder()
return true
}
func textView(descriptionTextView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if (text == "\n") {
descriptionTextView.resignFirstResponder()
return false
}
return true
}
}
"A new line is always added and I press 'next', even if there's already text or a new line." 在第一次按下“下一个”时,光标会自动定位在第二行。
![enter image description here](https://istack.dev59.com/66LHE.webp)
![enter image description here](https://istack.dev59.com/eQfJW.webp)