我在注册屏幕上有4个文本字段,我已将其设置为当用户在每个文本字段上按下一个时,下一个文本字段将被聚焦。代码如下:
func textFieldShouldReturn(textField: UITextField) -> Bool {
if (textField == self.fNameField) {
textField.resignFirstResponder()
self.sNameField.becomeFirstResponder()
}
else if (textField == self.sNameField) {
self.emailField.becomeFirstResponder()
} else if (textField == self.emailField) {
self.passwordField.becomeFirstResponder()
}
else{
var thereWereErrors = checkForErrors()
if !thereWereErrors
{
//conditionally segue to next screen
}
}
return true
}
在最终文本字段的返回时,我正在调用错误检查功能(如下)。在此范围内如果任何字段存在问题,我想要关注该文本字段,以便用户可以轻松地纠正它。发生的事情是带有错误的文本字段聚焦(按checkForErrors函数的指示)一秒钟,然后焦点切换回密码文本字段。我还尝试将self.passwordField.resignFirstResponder()
添加到上面函数的最后一个中,这使得密码字段失去焦点,但是遇到问题的文本字段根本没有获得焦点(甚至不像以前一样)我该如何解决这个问题?
func checkForErrors() -> Bool
{
var errors = false
let title = "Error"
var message = ""
if fNameField.text.isEmpty {
errors = true
message += "First name empty"
alertWithTitle(title, message: message, ViewController: self)
self.fNameField.becomeFirstResponder()
}
else if sNameField.text.isEmpty
{
errors = true
message += "Surname empty"
alertWithTitle(title, message: message, ViewController: self)
self.sNameField.becomeFirstResponder()
}
else if emailField.text.isEmpty
{
errors = true
message += "Email empty"
alertWithTitle(title, message: message, ViewController: self)
self.emailField.becomeFirstResponder()
}
else if !isValidEmail(emailField.text)
{
errors = true
message += "Invalid Email Address"
alertWithTitle(title, message: message, ViewController: self)
self.emailField.becomeFirstResponder()
}
else if passwordField.text.isEmpty
{
errors = true
message += "Password empty"
alertWithTitle(title, message: message, ViewController: self)
self.passwordField.becomeFirstResponder()
}
else if count(passwordField.text.utf16)<8
{
errors = true
message += "Password must be at least 8 characters"
alertWithTitle(title, message: message, ViewController: self)
self.passwordField.becomeFirstResponder()
}
return errors
}
注意我已经包含了textField委托。
根据要求提供标题功能的警报:
func alertWithTitle(title: String!, #message: String, #ViewController: UIViewController) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alert.addAction(action)
ViewController.presentViewController(alert, animated: true, completion: nil)
}
答案 0 :(得分:51)
这对我有用:
import UIKit
class ViewController:UIViewController, UITextFieldDelegate {
@IBOutlet weak var fNameField: UITextField!
@IBOutlet weak var sNameField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
fNameField.delegate = self
sNameField.delegate = self
emailField.delegate = self
passwordField.delegate = self
}
func isValidEmail (test:String) ->Bool{
// your email validation here...
return true
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
if (textField == self.fNameField) {
self.sNameField.becomeFirstResponder()
}
else if (textField == self.sNameField) {
self.emailField.becomeFirstResponder()
} else if (textField == self.emailField) {
self.passwordField.becomeFirstResponder()
}
else{
var thereWereErrors = checkForErrors()
if !thereWereErrors
{
//conditionally segue to next screen
}
}
return true
}
func checkForErrors() -> Bool
{
var errors = false
let title = "Error"
var message = ""
if fNameField.text.isEmpty {
errors = true
message += "First name empty"
alertWithTitle(title, message: message, ViewController: self, toFocus:self.fNameField)
}
else if sNameField.text.isEmpty
{
errors = true
message += "Surname empty"
alertWithTitle(title, message: message, ViewController: self, toFocus:self.sNameField)
self.sNameField.becomeFirstResponder()
}
else if emailField.text.isEmpty
{
errors = true
message += "Email empty"
alertWithTitle(title, message: message, ViewController: self, toFocus:self.emailField)
}
else if !isValidEmail(emailField.text)
{
errors = true
message += "Invalid Email Address"
alertWithTitle(title, message: message, ViewController: self, toFocus:self.emailField)
}
else if passwordField.text.isEmpty
{
errors = true
message += "Password empty"
alertWithTitle(title, message: message, ViewController: self, toFocus:passwordField)
}
else if count(passwordField.text.utf16)<8
{
errors = true
message += "Password must be at least 8 characters"
alertWithTitle(title, message: message, ViewController: self, toFocus:self.passwordField)
}
return errors
}
func alertWithTitle(title: String!, message: String, ViewController: UIViewController, toFocus:UITextField) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel,handler: {_ in
toFocus.becomeFirstResponder()
});
alert.addAction(action)
ViewController.presentViewController(alert, animated: true, completion:nil)
}
}
答案 1 :(得分:46)
theTextFieldYouWant.becomeFirstResponder()