TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Feb 2016 |
SPMSupports SPM | ✗ |
Maintained by sgr-ksmt.
Easy to move from UITextField/UITextView to next UITextField/UITextView written in Swift.
Some ViewController have four forms(text field or text view) like nameTextField
, emailTextField
, passwordTextField
and profileTextView
.
When they enter their name in and press ‘Next’ on the keyboard, I want this to automatically move to the next Form.
So, I will implement like below:
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet private weak var nameTextField: UITextField!
@IBOutlet private weak var mailTextField: UITextField!
@IBOutlet private weak var passwordTextField: UITextField!
@IBOutlet private weak var profileTextView: UITextView!
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
if textField == nameTextField {
mailTextField.becomeFirstResponder()
} else if textField == mailTextField {
passwordTextField.becomeFirstResponder()
} else if textField == passwordTextField {
profileTextView.becomeFirstResponder()
} else {
return true
}
return false
}
}
or,
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet private weak var nameTextField: UITextField!
@IBOutlet private weak var mailTextField: UITextField!
@IBOutlet private weak var passwordTextField: UITextField!
private var textfields = [UITextField]()
override func viewDidLoad() {
super.viewDidLoad()
textfields += [nameTextField, mailTextField, passwordTextField]
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
let tag = textField.tag
let nextIndex = tag + 1
if nextIndex < textfields.count {
textfields[nextIndex].becomeFirstResponder()
return false
}
return true
}
}
…It’s not very good.
They are bother!!!
FormChangeable
can resolve these problems.
UITextFIeld
and UITextView
import FormChangeable
class ViewController: UIViewController {
@IBOutlet private weak var nameTextField: UITextField!
@IBOutlet private weak var mailTextField: UITextField!
@IBOutlet private weak var passwordTextField: UITextField!
@IBOutlet private weak var profileTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
nameTextField.nextForm = mailTextField
mailTextField.nextForm = passwordTextField
passwordTextField.nextForm = profileTextView
}
}
or,
import FormChangeable
class ViewController: UIViewController {
@IBOutlet private weak var nameTextField: UITextField!
@IBOutlet private weak var mailTextField: UITextField!
@IBOutlet private weak var passwordTextField: UITextField!
@IBOutlet private weak var profileTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let forms: [FormChangeable] = [nameTextField, mailTextField, passwordTextField, profileTextView]
forms.registerNextForm()
}
}
UITextFieldDelegate
’s func textFieldShouldReturn(textField:) UITextViewDelegate
s func textView(textView:, shouldChangeTextInRange:, replacementText:) class ViewController: UIViewController, UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.changeToNextForm()
return false
}
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
textView.changeToNextForm()
return false
}
return true
}
}
FormChangeable is under MIT license. See the LICENSE file for more info.