Ios 在内置键盘上单击“删除”按钮时,如何移动到上一个文本字段
我正在构建一个iOS应用程序,需要用户输入由4位数字组成的pin码。因此,我分别创建了4个TextField,以便为每个Ios 在内置键盘上单击“删除”按钮时,如何移动到上一个文本字段,ios,objective-c,swift,uitextfield,Ios,Objective C,Swift,Uitextfield,我正在构建一个iOS应用程序,需要用户输入由4位数字组成的pin码。因此,我分别创建了4个TextField,以便为每个TextField接受一个数字。当用户输入每个文本字段并顺利地从文本字段向前移动到另一个文本字段时,它工作正常 但问题是,我希望用户能够通过单击iOS内置键盘提供的清除按钮图标来删除。当用户点击该图标按钮时,它应该让textfield移动到上一个textfield,但它对我不起作用 我在stackoverflow上找到了很多在线资源,但它仍然不适合我。这就是为什么我提出了自己的
TextField
接受一个数字。当用户输入每个文本字段并顺利地从文本字段向前移动到另一个文本字段时,它工作正常
但问题是,我希望用户能够通过单击iOS内置键盘提供的清除按钮图标来删除。当用户点击该图标按钮时,它应该让textfield移动到上一个textfield,但它对我不起作用
我在stackoverflow
上找到了很多在线资源,但它仍然不适合我。这就是为什么我提出了自己的问题
这就是我的代码的样子!我以编程方式创建了textfield
self.coverView.addSubview(self.paymentView.firstDigit)
self.coverView.addSubview(self.paymentView.secondDigit)
self.coverView.addSubview(self.paymentView.thirdDigit)
self.coverView.addSubview(self.paymentView.fourthDigit)
self.view.addSubview(self.overlayView)
self.overlayView.snp.makeConstraints { (make) in
make.top.width.height.centerX.equalTo(self.coverView)
}
self.paymentView.firstDigit.becomeFirstResponder()
self.paymentView.firstDigit.snp.makeConstraints { (make) in
make.top.equalTo(self.coverView)
make.width.height.equalTo(21)
make.leading.equalTo(self.coverView)
}
self.paymentView.secondDigit.snp.makeConstraints { (make) in
make.top.equalTo(self.coverView)
make.leading.equalTo(self.paymentView.firstDigit.snp.trailing).offset(8)
make.width.height.equalTo(21)
}
self.paymentView.thirdDigit.snp.makeConstraints { (make) in
make.top.equalTo(self.coverView)
make.leading.equalTo(self.paymentView.secondDigit.snp.trailing).offset(8)
make.width.height.equalTo(21)
}
self.paymentView.fourthDigit.snp.makeConstraints { (make) in
make.top.equalTo(self.coverView)
make.leading.equalTo(self.paymentView.thirdDigit.snp.trailing).offset(8)
make.width.height.equalTo(21)
}
self.paymentView.firstDigit.delegate = self
self.paymentView.secondDigit.delegate = self
self.paymentView.thirdDigit.delegate = self
self.paymentView.fourthDigit.delegate = self
上面的代码包括TextField的委托。下面是我如何为每个文本字段设置目标
self.paymentView.firstDigit.addTarget(self, action: #selector(textfieldDidChange(textField:)), for: .editingChanged)
self.paymentView.secondDigit.addTarget(self, action: #selector(textfieldDidChange(textField:)), for: .editingChanged)
self.paymentView.thirdDigit.addTarget(self, action: #selector(textfieldDidChange(textField:)), for: .editingChanged)
self.paymentView.fourthDigit.addTarget(self, action: #selector(textfieldDidChange(textField:)), for: .editingChanged)
下面是textfieldDidChange的功能
@objc func textfieldDidChange(textField: UITextField) {
let text = textField.text
print("This is an amount of text ", text?.count)
if text?.count == 1 {
switch textField {
case self.paymentView.firstDigit:
self.paymentView.firstDigit.textColor = BaseColor.colorPrimary
self.paymentView.firstDigit.backgroundColor = BaseColor.colorPrimary
self.paymentView.secondDigit.becomeFirstResponder()
case self.paymentView.secondDigit:
self.paymentView.secondDigit.textColor = BaseColor.colorPrimary
self.paymentView.thirdDigit.becomeFirstResponder()
self.paymentView.secondDigit.backgroundColor = BaseColor.colorPrimary
case self.paymentView.thirdDigit:
self.paymentView.thirdDigit.textColor = BaseColor.colorPrimary
self.paymentView.fourthDigit.becomeFirstResponder()
self.paymentView.thirdDigit.backgroundColor = BaseColor.colorPrimary
case self.paymentView.fourthDigit:
self.paymentView.fourthDigit.textColor = BaseColor.colorPrimary
self.paymentView.fourthDigit.backgroundColor = BaseColor.colorPrimary
self.paymentView.fourthDigit.resignFirstResponder()
self.view.endEditing(true)
default:
break
}
}
if text?.count == 0 {
switch textField {
case self.paymentView.firstDigit:
self.paymentView.firstDigit.becomeFirstResponder()
self.paymentView.firstDigit.backgroundColor = .red
case self.paymentView.secondDigit:
self.paymentView.firstDigit.becomeFirstResponder()
self.paymentView.firstDigit.backgroundColor = .red
case self.paymentView.thirdDigit:
self.paymentView.secondDigit.becomeFirstResponder()
self.paymentView.secondDigit.backgroundColor = .red
case self.paymentView.fourthDigit:
self.paymentView.thirdDigit.becomeFirstResponder()
self.paymentView.thirdDigit.backgroundColor = .red
default:
break
}
}
}
上面是我如何试图让textfield移动到上一个textfield时,内置键盘iOS的clear按钮被点击,但它没有移动。上面的代码是我从源代码中复制的我的方法是这样的:
import UIKit
class ViewController1: UIViewController, UITextFieldDelegate {
@IBOutlet weak var text1: UITextField!
@IBOutlet weak var text2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
text1.delegate = self
text2.delegate = self
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
if textField == text1 {
textField.text = ""
textField.resignFirstResponder()
text2.becomeFirstResponder()
}
return false
}
}
我已经完成了这项工作,请按照以下步骤操作,您将根据需要获得结果:
@IBOutlet weak var txt4: UITextField!
@IBOutlet weak var txt3: UITextField!
@IBOutlet weak var txt2: UITextField!
@IBOutlet weak var txt1: UITextField!
在视图控制器中添加textField委托
在viewDidLoad中指定代理:
txt1.delegate = self
txt2.delegate = self
txt3.delegate = self
txt4.delegate = self
然后添加此代码,您将得到想要的结果
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if (textField == txt1) {
txt2.becomeFirstResponder()
}else if (textField == txt2) {
txt3.becomeFirstResponder()
}else if (textField == txt3) {
txt4.becomeFirstResponder()
}else if (textField == txt4) {
//here
var str = self.txt1.text! + self.txt2.text! + self.txt3.text! + self.txt4.text!
str = str.replacingOccurrences(of: " ", with: "")
self.verify(otp: str)
// self.navigationController?.pushViewController(vc, animated: true)
}
return true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if(textField.text?.count == 0)
{
textField.text = " "
}
}
@objc func setNextResponder(textfield : UITextField)
{
textfield.becomeFirstResponder()
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let _char = string.cString(using: .utf8)
// const char * _char = [string cStringUsingEncoding:NSUTF8StringEncoding];
// int isBackSpace = strcmp(_char, "\b");
let isBackSpace = strcmp(_char, "\\b")
if (isBackSpace == -92) {
// NSLog(@"Backspace was pressed");
if (textField == txt4)
{
if(textField.text!.count == 2)
{
}
else{
self.perform(#selector(setNextResponder), with: txt3, afterDelay: 0.01)
txt3.text = " "
}
}
else if (textField == txt3)
{
if(textField.text!.count == 2)
{
}
else{
self.perform(#selector(setNextResponder), with: txt2, afterDelay: 0.01)
txt2.text = " "
}
}
else if (textField == txt2)
{
if(textField.text!.count == 2)
{
}
else{
self.perform(#selector(setNextResponder), with: txt1, afterDelay: 0.01)
txt1.text = " "
}
}
else if (textField == txt1)
{
if(textField.text!.count == 2)
{
}
else{
textField.resignFirstResponder()
}
}
}
if (string.count > 1 && !(Scanner.init(string: string).scanInt(nil)))
{
return false;
}
let oldLength = textField.text!.count
let replacementLength = string.count
let rangeLength = range.length
let newLength = oldLength - rangeLength + replacementLength
// This 'tabs' to next field when entering digits
if (newLength == 2) {
if (textField == txt1)
{
self.perform(#selector(setNextResponder), with: txt2, afterDelay: 0.01)
}
else if (textField == txt2)
{
self.perform(#selector(setNextResponder), with: txt3, afterDelay: 0.01)
}
else if (textField == txt3)
{
self.perform(#selector(setNextResponder), with: txt4, afterDelay: 0.01)
}
else if(textField == txt4)
{
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
// let vc = self.storyboard?.instantiateViewController(withIdentifier: "navVC") as! NavigationViewController
// vc.modalPresentationStyle = .fullScreen
// self.present(vc, animated: true, completion: nil)
var str = self.txt1.text! + self.txt2.text! + self.txt3.text! + self.txt4.text!
str = str.replacingOccurrences(of: " ", with: "")
self.verify(otp: str)
}
}
}
return newLength <= 2;
}
func textField应该返回(\utextfield:UITextField)->Bool{
如果(textField==txt1){
txt2.成为第一响应者()
}else if(textField==txt2){
txt3.成为第一响应者()
}else if(textField==txt3){
txt4.成为第一响应者()
}else if(textField==txt4){
//这里
var str=self.txt1.text!+self.txt2.text!+self.txt3.text!+self.txt4.text!
str=str.replacingOccurrences(of:,with:)
自我验证(otp:str)
//self.navigationController?.pushViewController(vc,动画:true)
}
返回真值
}
func textField didbeginediting(textField:UITextField){
if(textField.text?.count==0)
{
textField.text=“”
}
}
@objc func setNextResponder(文本字段:UITextField)
{
textfield.becomeFirstResponder()
}
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
让_char=string.cString(使用:.utf8)
//const char*_char=[string cStringUsingEncoding:NSUTF8StringEncoding];
//int isBackSpace=strcmp(_char,“\b”);
让isBackSpace=strcmp(\u char,\\b)
如果(isBackSpace==-92){
//NSLog(@“已按退格”);
如果(textField==txt4)
{
如果(textField.text!.count==2)
{
}
否则{
自我执行(#选择器(setNextResponder),带:txt3,后延迟:0.01)
txt3.text=“”
}
}
else if(textField==txt3)
{
如果(textField.text!.count==2)
{
}
否则{
自我执行(#选择器(setNextResponder),带:txt2,后延迟:0.01)
txt2.text=“”
}
}
else if(textField==txt2)
{
如果(textField.text!.count==2)
{
}
否则{
自我执行(#选择器(setNextResponder),带:txt1,后延迟:0.01)
txt1.text=“”
}
}
else if(textField==txt1)
{
如果(textField.text!.count==2)
{
}
否则{
textField.resignFirstResponder()辞职
}
}
}
if(string.count>1&&!(Scanner.init(string:string.scannit(nil)))
{
返回false;
}
让oldLength=textField.text!.count
让replacementLength=string.count
让rangeLength=range.length
设newLength=oldLength-rangeLength+replacementLength
//输入数字时,此“制表符”将转到下一个字段
如果(newLength==2){
如果(textField==txt1)
{
自我执行(#选择器(setNextResponder),带:txt2,后延迟:0.01)
}
else if(textField==txt2)
{
自我执行(#选择器(setNextResponder),带:txt3,后延迟:0.01)
}
else if(textField==txt3)
{
自我执行(#选择器(setNextResponder),带:txt4,后延迟:0.01)
}
else if(textField==txt4)
{
DispatchQueue.main.asyncAfter(截止日期:.now()+0.5){
//将vc=self.storyboard?.instantialeviewController(标识符为:“navVC”)设为!NavigationViewController
//vc.modalPresentationStyle=.fullScreen
//self.present(vc,动画:true,完成:nil)
var str=self.txt1.text!+self.txt2.text!+self.txt3.text!+self.txt4.text!
str=str.replacingOccurrences(of:,with:)
自我验证(otp:str)
}
}
}
返回newLength