“Resign” and “Become” first responder — UITextFieldDelegate example

When we first begin using a new downloaded app, the very first step when we have to do when we enter the app was usually be login. When it comes to login page, there always have two basic field for us to insert login info, those are “UserID” and “PassWord” (They might exist as some same meaning words).

A really simple but important design for raising User Experience was automatically jump to next column when we pressing Next(or return) button. Without this function, users would have to click the next textfield they need to inert for theirselfs, this extra move will absolutly be a bad UX! And also, Now here is how I done this page, not only the write codes but we lso have to let the item connected to delegate (Click here to see my last Medium article to learn how to do it!) you can see the following codes.

import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var idTxt: UITextField!
@IBOutlet weak var pwdTxt: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pwdTxt.isSecureTextEntry = true
idTxt.returnKeyType = .next
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField.text! == "" {
return false
} else if textField == idTxt {
textField.resignFirstResponder()
pwdTxt.becomeFirstResponder()
return true
} else if textField == pwdTxt {
textField.resignFirstResponder()
return true
}else {
return false
}
}
}

After reading my codes, you can figured you that I made some condition control to prevent empty string and replaced the password as ***

If you want to download my full project, just follow my github

https://github.com/imbearfly/JumpToNext

Like what you read? Give Fly Hsiung a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.