制作类似于锁定屏幕的登录屏幕
Making a Login screen similar to the lock screen
我正在尝试使用 4 个文本字段制作类似于锁定屏幕的登录屏幕。
我面临的问题是将焦点从一个文本字段更改为下一个文本字段。
- 当我说
secondTextField.becomeFirstResponder()
时,firstTextField 的值被复制到 secondTextField
- 我正在使用小数点键盘类型
这是我到目前为止所做的,但我找不到解决问题的方法:
- 使用 UITextFieldDelegate
- 为所有文本字段建立了 IBOutlet 连接
firstText.delegate = self
secondText.delegate = self
thirdText.delegate = self
firstText.becomeFirstResponder()
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
var newLength = countElements(textField.text) + countElements(字符串) - range.length
if newLength == 1 {
if textField == firstText{
changeFocus(textField)
}else if textField == secondText {
changeFocus(textField)
}else if textField == thirdText {
changeFocus(textField)
}
}
return newLength <= 1
}
func changeFocus(nextFocus: UITextField){
if nextFocus == firstText {
firstText.resignFirstResponder()
secondText.becomeFirstResponder()
} else if nextFocus == secondText {
secondText.resignFirstResponder()
thirdText.becomeFirstResponder()
} else if nextFocus == thirdText {
thirdText.resignFirstResponder()
}
}
我怎样才能正确地做到这一点,请提供任何帮助和建议。
使用此委托方法更改焦点
func textFieldShouldReturn(textField: UITextField) -> Bool
要向 svrushal 添加一点答案:
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == firstText {
secondText.becomeFirstResponder()
}
if textField == secondText {
thirdText.becomeFirstResponder()
}
if textField == thirdText {
fourthText.becomeFirstResponder()
}
}
你可以参考这个linkhttps://github.com/dhennessy/PAPasscode/blob/master/PAPasscode/PAPasscodeViewController.h
它会帮助你。
我在这里找到了类似的东西:similar
成功运行,但有两个限制:
- 首先,我无法向后删除
- 其次,如果密码错误,用户必须点击第一个文本字段。
但现在这些已经足够好了。
方法如下
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
var shouldProcess = false
var moveToNextTextField = false
var insertStringLength = countElements(string)
if insertStringLength == 0 {
shouldProcess = true
}
else {
if countElements(textField.text) == 0 {
shouldProcess = true
}
}
if shouldProcess {
var myString = textField.text
if countElements(myString) == 0{
myString = myString + string
password = password + myString
moveToNextTextField = true
}
textField.text = myString
if moveToNextTextField {
changeFocus(textField)
}
}
return false
}
改变焦点的方法:
func changeFocus(nextTextField : UITextField){
if nextTextField == firstText {
firstText.resignFirstResponder()
secondText.becomeFirstResponder()
} else if nextTextField == secondText {
secondText.resignFirstResponder()
thirdText.becomeFirstResponder()
} else if nextTextField == thirdText {
thirdText.resignFirstResponder()
fourthText.becomeFirstResponder()
} else if nextTextField == fourthText {
fourthText.resignFirstResponder()
}
}
检查方法 pin/passcode :
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
if textField == fourthText {
thirdText.resignFirstResponder()
let pass = defaultUser.stringForKey(Admin)
println("password = \(password)")
if pass == password {
println("the password is correct")
dispatch_async(dispatch_get_main_queue()) { () -> Void in
self.performSegueWithIdentifier("SegueToSecond", sender: self)
}
} else {
println("wrong password")
thirdText.text = ""
secondText.text = ""
firstText.text = ""
fourthText.text = ""
password = ""
view.reloadInputViews()
}
}
return true
}
希望这些对以后的人有所帮助。
我正在尝试使用 4 个文本字段制作类似于锁定屏幕的登录屏幕。 我面临的问题是将焦点从一个文本字段更改为下一个文本字段。
- 当我说
secondTextField.becomeFirstResponder()
时,firstTextField 的值被复制到 secondTextField - 我正在使用小数点键盘类型
这是我到目前为止所做的,但我找不到解决问题的方法:
- 使用 UITextFieldDelegate
- 为所有文本字段建立了 IBOutlet 连接
firstText.delegate = self secondText.delegate = self thirdText.delegate = self firstText.becomeFirstResponder()
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { var newLength = countElements(textField.text) + countElements(字符串) - range.length
if newLength == 1 { if textField == firstText{ changeFocus(textField) }else if textField == secondText { changeFocus(textField) }else if textField == thirdText { changeFocus(textField) } } return newLength <= 1
}
func changeFocus(nextFocus: UITextField){
if nextFocus == firstText { firstText.resignFirstResponder() secondText.becomeFirstResponder() } else if nextFocus == secondText { secondText.resignFirstResponder() thirdText.becomeFirstResponder() } else if nextFocus == thirdText { thirdText.resignFirstResponder() }
}
我怎样才能正确地做到这一点,请提供任何帮助和建议。
使用此委托方法更改焦点
func textFieldShouldReturn(textField: UITextField) -> Bool
要向 svrushal 添加一点答案:
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == firstText {
secondText.becomeFirstResponder()
}
if textField == secondText {
thirdText.becomeFirstResponder()
}
if textField == thirdText {
fourthText.becomeFirstResponder()
}
}
你可以参考这个linkhttps://github.com/dhennessy/PAPasscode/blob/master/PAPasscode/PAPasscodeViewController.h
它会帮助你。
我在这里找到了类似的东西:similar
成功运行,但有两个限制:
- 首先,我无法向后删除
- 其次,如果密码错误,用户必须点击第一个文本字段。 但现在这些已经足够好了。
方法如下
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { var shouldProcess = false var moveToNextTextField = false var insertStringLength = countElements(string) if insertStringLength == 0 { shouldProcess = true } else { if countElements(textField.text) == 0 { shouldProcess = true } } if shouldProcess { var myString = textField.text if countElements(myString) == 0{ myString = myString + string password = password + myString moveToNextTextField = true } textField.text = myString if moveToNextTextField { changeFocus(textField) } } return false }
改变焦点的方法:
func changeFocus(nextTextField : UITextField){ if nextTextField == firstText { firstText.resignFirstResponder() secondText.becomeFirstResponder() } else if nextTextField == secondText { secondText.resignFirstResponder() thirdText.becomeFirstResponder() } else if nextTextField == thirdText { thirdText.resignFirstResponder() fourthText.becomeFirstResponder() } else if nextTextField == fourthText { fourthText.resignFirstResponder() }
}
检查方法 pin/passcode :
func textFieldShouldEndEditing(textField: UITextField) -> Bool { if textField == fourthText { thirdText.resignFirstResponder() let pass = defaultUser.stringForKey(Admin) println("password = \(password)") if pass == password { println("the password is correct") dispatch_async(dispatch_get_main_queue()) { () -> Void in self.performSegueWithIdentifier("SegueToSecond", sender: self) } } else { println("wrong password") thirdText.text = "" secondText.text = "" firstText.text = "" fourthText.text = "" password = "" view.reloadInputViews() } } return true }
希望这些对以后的人有所帮助。