我对iOS / Swift很陌生,在设置UITextView
作为第一响应者时(从上一个next
触摸UITextField
时),我有一种奇怪的行为。它会自动在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
}
}
答案 0 :(得分:10)
在textFieldShouldReturn
func textFieldShouldReturn(nameTextField: UITextField) -> Bool {
nameTextField.resignFirstResponder()
descriptionTextView.becomeFirstResponder()
return false
}
更多详情https://dl.google.com/android/repository/platform-tools_r23.0.1-linux.zip