16

我有一个Verification ViewController,我通过短信获得 4 位验证码,我需要输入这些验证码才能登录,我创建了ViewController这样的

如您所见,四个UITextFields,我只需要为每个 s 允许一个数字UITextField

我尝试了什么:我试图使用shouldChangeCharactersInRange:method:,但它没有被调用,我不知道出了什么问题,我认为因为UITextFields 在UITableView所以它不起作用。

4

14 回答 14

26

您可以通过使用文本字段的委托功能来更改文本字段。最初,您需要设置每个文本字段的委托和标签。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ((textField.text.length >= 1) && (string.length > 0))
    {
        NSInteger nextTag = textField.tag + 1;
        // Try to find next responder
        UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
        if (! nextResponder)
            nextResponder = [textField.superview viewWithTag:1];

        if (nextResponder)
           // Found next responder, so set it.
           [nextResponder becomeFirstResponder];

        return NO;
    }
    return YES;
}

斯威夫特 2

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    // On inputing value to textfield
    if (textField.text?.characters.count < 1  && string.characters.count > 0){
        let nextTag = textField.tag + 1;

        // get next responder
        var nextResponder = textField.superview?.viewWithTag(nextTag);

        if (nextResponder == nil){
            nextResponder = textField.superview?.viewWithTag(1);
        }
        textField.text = string;
        nextResponder?.becomeFirstResponder();
        return false;
    }
    else if (textField.text?.characters.count >= 1  && string.characters.count == 0){
        // on deleting value from Textfield
        let previousTag = textField.tag - 1;

        // get next responder
        var previousResponder = textField.superview?.viewWithTag(previousTag);

        if (previousResponder == nil){
            previousResponder = textField.superview?.viewWithTag(1);
        }
        textField.text = "";
        previousResponder?.becomeFirstResponder();
        return false;
    }
    return true;
}

斯威夫特 4

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    if textField.text!.count < 1  && string.count > 0{
        let nextTag = textField.tag + 1

        // get next responder
        var nextResponder = textField.superview?.viewWithTag(nextTag)

        if (nextResponder == nil){

            nextResponder = textField.superview?.viewWithTag(1)
        }
        textField.text = string
        nextResponder?.becomeFirstResponder()
        return false
    }
    else if textField.text!.count >= 1  && string.count == 0{
        // on deleting value from Textfield
        let previousTag = textField.tag - 1

        // get next responder
        var previousResponder = textField.superview?.viewWithTag(previousTag)

        if (previousResponder == nil){
            previousResponder = textField.superview?.viewWithTag(1)
        }
        textField.text = ""
        previousResponder?.becomeFirstResponder()
        return false
    }
    return true

}
于 2013-09-10T06:01:07.813 回答
12

如果您不想使用标签,请使用此代码,它比上面的效果更好

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        // On inputing value to textfield
        if ((textField.text?.characters.count)! < 1  && string.characters.count > 0){
            if(textField == txtOne)
            {
                txtTwo.becomeFirstResponder()
            }
            if(textField == txtTwo)
            {
                txtThree.becomeFirstResponder()
            }
            if(textField == txtThree)
            {
                txtFour.becomeFirstResponder()
            }

            textField.text = string
            return false
        }
        else if ((textField.text?.characters.count)! >= 1  && string.characters.count == 0){
            // on deleting value from Textfield
            if(textField == txtTwo)
            {
                txtOne.becomeFirstResponder()
            }
            if(textField == txtThree)
            {
                txtTwo.becomeFirstResponder()
            }
            if(textField == txtFour)
            {
                txtThree.becomeFirstResponder()
            }
            textField.text = ""
            return false
        }
        else if ((textField.text?.characters.count)! >= 1  )
        {
            textField.text = string
            return false
        }
        return true
    }
于 2017-03-31T10:02:23.280 回答
3

我已经为两个图像拍摄了一个隐藏文本字段和四个图像视图。一个用于空白,另一个用于 Bullet,与 iOS 默认设置相同。

还为四个图像视图设置标签。

On Load set Focus for Pin Code

- (void)startPinCode
{
    txtPinCodeLockDigits.text = @"";

    for (int i = 1; i <= 4; i++) {

       UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
       [img setImage:[UIImage imageNamed:@"Img_BG_PinCode.png"]];
    }

    [txtPinCodeLockDigits becomeFirstResponder];
}

然后根据用户输入更改 imageview 的图像,并且只允许四个字符

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
    textField.text = result;

    for (int i = 1; i <= 4; i++) {

       UIImageView *img = (UIImageView *)[self.view viewWithTag:i];
       if (i <= [result length])
           [img setImage:[UIImage imageNamed:@"Img_BG_PinCode_Filled.png"]];
       else
           [img setImage:[UIImage imageNamed:@"Img_BG_PinCode.png"]];
     }

     NSLog(@"Result :: %@", result);

     if ([result length] == 4) {
        [self performSelector:@selector(keyGenerationForApplication:) withObject:result afterDelay:0.2];
     }

     return NO;
}

生成 PIN 码的四个字符后调用函数并将其存储在与 iOS 默认 PIN 设置相同的用户默认值中

- (void)keyGenerationForApplication:(NSString *)pinCode
{
     int appCode = [pinCode intValue];
     [DefaultsValues setIntegerValueToUserDefaults:appCode ForKey:PIN_LOCK_PATTERN];
}

在这里,您可以再次调用StartPinCode方法重新确认代码。

希望它会帮助你。
谢谢

于 2013-09-10T05:58:41.490 回答
3

斯威夫特 4

灵感来自@Anurag Soni 和@Varun Naharia 答案

变体 A

extension EnterConfirmationCodeTextField: UITextFieldDelegate {

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        guard let textFieldCount = textField.text?.count else { return false }

        // Сlosure
        let setValueAndMoveForward = {
            textField.text = string
            let nextTag = textField.tag + 1
            if let nextResponder = textField.superview?.viewWithTag(nextTag) {
                nextResponder.becomeFirstResponder()
            }
        }

        // Сlosure
        let clearValueAndMoveBack = {
            textField.text = ""
            let previousTag = textField.tag - 1
            if let previousResponder = textField.superview?.viewWithTag(previousTag) {
                previousResponder.becomeFirstResponder()
            }
        }

        if textFieldCount < 1 && string.count > 0 {

            setValueAndMoveForward()

            if textField.tag == 4 {
                print("Do something")
            }

            return false

        } else if textFieldCount >= 1 && string.count == 0 {

            clearValueAndMoveBack()
            return false

        } else if textFieldCount >= 1 && string.count > 0 {

            let nextTag = self.tag + 1
            if let previousResponder = self.superview?.viewWithTag(nextTag) {
                previousResponder.becomeFirstResponder()

                if let activeTextField = previousResponder as? UITextField {
                    activeTextField.text = string
                }
            }

            return false
        }

        return true

    }

}

变体 B(有点另一种行为):

extension EnterConfirmationCodeTextField: UITextFieldDelegate {

        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            guard let textFieldCount = textField.text?.count else { return false }

            // Сlosure
            let setValueAndMoveForward = {
                textField.text = string
                let nextTag = textField.tag + 1
                if let nextResponder = textField.superview?.viewWithTag(nextTag) {
                    nextResponder.becomeFirstResponder()
                }
            }

            // Сlosure
            let clearValueAndMoveBack = {
                textField.text = ""
                let previousTag = textField.tag - 1
                if let previousResponder = textField.superview?.viewWithTag(previousTag) {
                    previousResponder.becomeFirstResponder()
                }
            }

            if textFieldCount < 1 && string.count > 0 {

                setValueAndMoveForward()

                if textField.tag == 4 {
                    print("Do something")
                }

                return false

            } else if textFieldCount >= 1 && string.count == 0 {

                clearValueAndMoveBack()
                return false

            } else if textFieldCount >= 1 {

                setValueAndMoveForward()
                return false
            }

            return true

        }

 }

另外,我实现了这个功能:

在此处输入图像描述

在最后一个textFiled为空的情况下,我只想切换到上一个textFiled。我尝试了所有这些方法。但对我来说,下面的方法更优雅,而且很有魅力:

变体 A

class EnterConfirmationCodeTextField: UITextField {

    // MARK: Life cycle

    override func awakeFromNib() {
        super.awakeFromNib()

        delegate = self
    }

    // MARK: Methods

    override func deleteBackward() {
        super.deleteBackward()

        let previousTag = self.tag - 1
        if let previousResponder = self.superview?.viewWithTag(previousTag) {
            previousResponder.becomeFirstResponder()

            if let activeTextField = previousResponder as? UITextField {
                if let isEmpty = activeTextField.text?.isEmpty, !isEmpty {
                    activeTextField.text = String()
                }
            }
        }
    }

}

变体 B(有点另一种行为):

class EnterConfirmationCodeTextField: UITextField {

    // MARK: Life cycle

    override func awakeFromNib() {
        super.awakeFromNib()

        delegate = self
    }

    // MARK: Methods

    override func deleteBackward() {
        super.deleteBackward()

        let previousTag = self.tag - 1
        if let previousResponder = self.superview?.viewWithTag(previousTag) {
            previousResponder.becomeFirstResponder()
        }
    }

}

EnterConfirmationCodeTextField为每个文本字段分配并设置它们适当的tag值。

于 2018-08-30T16:02:22.250 回答
3

它可以使用 UITextField 委托和通过按递增顺序(例如 1 - 4)为每个文本字段设置标记来实现,下面是解决问题的委托处理程序。

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        // On inputing value to textfield
        if (textField.text?.characters.count < 1  && string.characters.count > 0){
            let nextTag = textField.tag + 1;

            // get next responder
            var nextResponder = textField.superview?.viewWithTag(nextTag);

            if (nextResponder == nil){
                nextResponder = textField.superview?.viewWithTag(1);
            }
            textField.text = string;
            nextResponder?.becomeFirstResponder();
            return false;
        }
        else if (textField.text?.characters.count >= 1  && string.characters.count == 0){
            // on deleteing value from Textfield
            let previousTag = textField.tag - 1;

            // get next responder
            var previousResponder = textField.superview?.viewWithTag(previousTag);

            if (previousResponder == nil){
                previousResponder = textField.superview?.viewWithTag(1);
            }
            textField.text = "";
            previousResponder?.becomeFirstResponder();
            return false;
        }
        return true;
    }
于 2015-10-07T20:22:12.897 回答
3

迅捷2.3

      class BankDepositsWithOTPVC: UIViewController {

            let limitLength = 1  

        override func viewDidLoad() {
            super.viewDidLoad()
        }


        }

        // MARK: Textfield Validator

        extension BankDepositsWithOTPVC : UITextFieldDelegate {


            func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
                // On inputing value to textfield
                if (textField.text?.characters.count < 1  && string.characters.count > 0){
                    let nextTag = textField.tag + 1;

                    // get next responder
                    let nextResponder = textField.superview?.viewWithTag(nextTag);

                    if (nextResponder == nil){
                        textField.resignFirstResponder()
                      //  nextResponder = textField.superview?.viewWithTag(1);
                    }
                    textField.text = string;
                    nextResponder?.becomeFirstResponder();
                    return false;

                }else  if (textField.text?.characters.count >= 1  && string.characters.count > 0){
                                    // maximum 1 digit

textField.text = "";

                    let nextTag = textField.tag + 1;

                    // get next responder
                    let nextResponder = textField.superview?.viewWithTag(nextTag);

                    if (nextResponder == nil){
                        textField.resignFirstResponder()
                        //  nextResponder = textField.superview?.viewWithTag(1);
                    }
                    textField.text = string;
                    nextResponder?.becomeFirstResponder();
                    return false;
                }
                else if (textField.text?.characters.count >= 1  && string.characters.count == 0){
                    // on deleteing value from Textfield
                    let previousTag = textField.tag - 1;

                    // get next responder
                    var previousResponder = textField.superview?.viewWithTag(previousTag);

                    if (previousResponder == nil){
                        previousResponder = textField.superview?.viewWithTag(1);
                    }
                    textField.text = "";
                    previousResponder?.becomeFirstResponder();
                    return false;
                }
                //return true;

                guard let text = textField.text else { return true }
                let newLength = text.characters.count + string.characters.count - range.length
                return newLength <= limitLength

            }

        }

Objective-C

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    if ((textField.text.length < 1) && (string.length > 0))
    {

        NSInteger nextTag = textField.tag + 1;
        UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
        if (! nextResponder){
            [textField resignFirstResponder];
        }
        textField.text = string;
        if (nextResponder)
            [nextResponder becomeFirstResponder];

        return NO;

    }else if ((textField.text.length >= 1) && (string.length > 0)){
        //FOR MAXIMUM 1 TEXT

        NSInteger nextTag = textField.tag + 1;
        UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
        if (! nextResponder){
            [textField resignFirstResponder];
        }
        textField.text = string;
        if (nextResponder)
            [nextResponder becomeFirstResponder];

        return NO;
    }
    else if ((textField.text.length >= 1) && (string.length == 0)){
        // on deleteing value from Textfield

        NSInteger prevTag = textField.tag - 1;
        // Try to find prev responder
        UIResponder* prevResponder = [textField.superview viewWithTag:prevTag];
        if (! prevResponder){
            [textField resignFirstResponder];
        }
        textField.text = string;
        if (prevResponder)
            // Found next responder, so set it.
            [prevResponder becomeFirstResponder];

        return NO;
    }

      return YES;
}
于 2017-02-26T08:07:52.953 回答
3

修改了Anurag Soni在Swift 3中的回答。

  • 它假定您已命名出口集合textFields,并且文本字段已设置有序标签
  • 当文本字段中已经有一些数字并且当用户输入新内容时,它会增加大小写 - 数字被替换
  • 输入仅限于数字
  • 它可以防止粘贴多个数字

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        // Restrict to only digits
        let aSet = NSCharacterSet(charactersIn:"0123456789").inverted
        let compSepByCharInSet = string.components(separatedBy: aSet)
        let numberFiltered = compSepByCharInSet.joined(separator: "")
    
        if string != numberFiltered {
            return false
        }
    
        // Get the unwrapped text
        guard let text = textField.text else {
            return false
        }
    
        if (text.characters.count < 1  && string.characters.count == 1) {
            // New value to empty text field
            textField.text = string
    
            // Next responder
            if let someTextField = (textFields.filter { $0.tag == textField.tag + 1 }).first {
                someTextField.becomeFirstResponder()
            } else {
                view.endEditing(true)
            }
    
            return false
        } else if (text.characters.count >= 1  && string.characters.count == 0){
            // On deleting value from Textfield
            textField.text = ""
    
            // Previous responder
            if let someTextField = (textFields.filter { $0.tag == textField.tag - 1 }).first {
                someTextField.becomeFirstResponder()
            } else {
                view.endEditing(true)
            }
    
            return false
        } else if string.characters.count == 1 {
            // There's already some digit in text field
            // Replace it with new one
            textField.text = string
    
            // Next responder
            if let someTextField = (textFields.filter { $0.tag == textField.tag + 1 }).first {
                someTextField.becomeFirstResponder()
            } else {
                view.endEditing(true)
            }
        }
    
        return false
    }
    
于 2017-10-05T11:02:40.923 回答
2

在此处输入图像描述

斯威夫特 4

当您将密码中的数字输入文本字段时,您应该显示一个数字,然后下一个文本字段将成为第一响应者,因此在代码出现在文本视图后更改第一响应者

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    guard let textField = textField as? PinCodeTextField else { return true }

    if string == "" {// when the backward clicked, let it go :)
        return true
    }

    // when textfield is not empty, well, next number
    if textField.pinCode.count == textField.maxCount {
        becomeFirstResponder(after: textField)
        return false
    }

    if string.count > textField.maxCharacterCount {// the max character count should be 1
        return false
    }
    return true
}
// now the text field has been filled with a number
func textFieldCotentDidChange(_ textField: UITextField) {
    print("didchange")
    guard let textField = textField as? PinCodeTextField else { return }
    if textField.pinCode.count == 0 {
        becomeFirstResponder(before: textField)
    }

    // when textfield has been filled, ok! next!
    if textField.pinCode.count == textField.maxCharacterCount {
        becomeFirstResponder(after: textField)
    }
}

有关更多详细信息和简单演示,请参阅此链接

于 2018-11-10T14:03:07.717 回答
2

将标签提供给文本字段,如 1,2,3,4 并直接使用它

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool


    let next:NSInteger

    if string == "" {

        next = textField.tag - 1;

    }
    else{

        next = textField.tag + 1;

    }

    if (textField.text?.characters.count)! >= 1 {

        if textField.tag == 4 {

            if string == "" {

                textField.text = ""

                let temptf = self.view.viewWithTag(next) as! UITextField

                temptf.becomeFirstResponder()

                return false

            }
            else{

                if (textField.text?.characters.count)! > 1 {

                    let stringg = textField.text!
                    textField.text = stringg.replacingOccurrences(of: stringg, with: string)
                }
                return false
            }
        }
        else{
            if string == "" {

                textField.text = ""

                if next != 0 {

                    let temptf = self.view.viewWithTag(next) as! UITextField
                    temptf.becomeFirstResponder()

                }

                return false
            }

            else{

                if (textField.text?.characters.count)! > 1 {


                    let stringg = textField.text!
                    textField.text = stringg.replacingOccurrences(of: stringg, with: string)

                }

                    let temptf = self.view.viewWithTag(next) as! UITextField

                    temptf.becomeFirstResponder()

            }
        }
    }

    return true

}
于 2017-03-03T07:04:18.140 回答
1

试试这个示例教程密码锁

视图控制器.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>
{
 IBOutlet UITextField *txtPassword;
}
 @end

视图控制器.m

- (void)viewDidLoad
{
[super viewDidLoad];
txtPassword.delegate=self;
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 1) ? NO : YES;
}
于 2013-09-10T06:22:08.507 回答
0

感谢@Anurag Soni,这是一个替代的 UX 推送到下一个字段并在完成后关闭(上面还有布局逻辑):

-(UIView *)addSpacedTextInputToView:(UIView *)parent subtitle:(NSString *)subtitle spaces:(int)spaces atOffset:(float)offset{
    
    float parentWidth = parent.frame.size.width;
    float tfWidth = 2 * increment;
    float tfHeight = 3 * increment;
    float startX = 3 * increment;
    float remainingWidth = parentWidth - 2 * startX - spaces * tfWidth;
    float padding = remainingWidth / (float)(spaces-1);
    
    UIView * holder = [self addViewToView:parent withFrame:CGRectMake(0, offset, parentWidth, 0)];
    UIView * tfHolder = [self addViewToView:holder withFrame:CGRectMake(0, 0, parentWidth, tfHeight)];
    
    float localX = startX;
    float localY = 0.0f;
    for (int n = 0; n < spaces; n++){
        
        UITextField * tf = [UITextField new];
        tf.frame = CGRectMake(localX, localY, tfWidth, tfHeight);
        tf.textColor = [UIColor whiteColor];
        tf.font = headerFont;
        tf.textAlignment = NSTextAlignmentCenter;
        tf.tintColor = [UIColor whiteColor];
        tf.keyboardAppearance = UIKeyboardAppearanceDark;
        tf.tag = n;
        tf.delegate = self;
        [tfHolder addSubview:tf];
        
        UIView * div = [self addViewToView:tf withFrame:CGRectMake(0, tfHeight-2, tfWidth, 1)];
        div.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5f];
        
        localX += tfWidth;
        localX += padding;
        
    }
    
    localY += tfHeight;
    localY += increment;
    localY += [self addSystemLabelToView:holder colour:nil text:subtitle maxWidth:0 offset:localY].frame.size.height;
    holder.frame = CGRectMake(0, offset, parent.frame.size.width, localY);
    
    return holder;
    
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    if (string.length > 0){
       
        textField.text = string.uppercaseString; //optional to uppercase
      
        UIView * div = textField.subviews.firstObject; //change div colour
        div.backgroundColor = [UIColor whiteColor];
        
        int nextTag = (int)textField.tag + 1;
        UIResponder * nextResponder = [textField.superview viewWithTag:nextTag];
        if (!nextResponder){
            [textField resignFirstResponder];
        }
        if (nextResponder){
           [nextResponder becomeFirstResponder];
        }
    }
    
    return false;
}
于 2022-01-27T07:04:42.887 回答
0

我正在研究类似的功能,并以我的方式做到了。下面的解决方案。 斯威夫特 4

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

 //For clear button pressed
 //If the textfield has already text in it
 if string.count == 0 {
    textField.text = string
    return true
 }

 //For First time entry into the text field
 guard let text = textField.text, text.count <= 0  else {
    //If user enter second character
    return false
 }
 //For First time entry into the text field
 if text.count == 0  {
     textField.text = string
     textField.resignFirstResponder()
     self.nextResponde(tag: textField.tag)
     return true
  }
  return false
}

//To make the next field as responder
func nextResponde(tag: Int) {
    switch tag {
    case self.PINTextField.tag:
        guard let text = self.PINTextField1.text, text.count == 1 else {
            self.PINTextField1.becomeFirstResponder()
            return
        }
    case self.PINTextField1.tag:
        guard let text = self.PINTextField2.text, text.count == 1 else {
            self.PINTextField2.becomeFirstResponder()
            return
        }
    case self.PINTextField2.tag:
        guard let text = self.PINTextField3.text, text.count == 1 else {
            self.PINTextField3.becomeFirstResponder()
            return
        }
    default:
        let _ = tag
    }
}
于 2018-10-24T10:22:26.600 回答
0

只需使用TextFieldDelegate方法并在每次更改后检查 TextField 的长度

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
    if newString.characters.count == 1
    {
        nextTextField.becomeFirstResponder()
        return true
    }
    else
    {
        return false
    }
}
于 2016-07-01T07:04:35.750 回答
-1

试试这个: -对于 swift 3.0

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    // On inputing value to textfield
    if ((textField.text?.characters.count)! < 1  && string.characters.count > 0){
        let nextTag = textField.tag + 1;

        // get next responder
        let nextResponder = textField.superview?.viewWithTag(nextTag);
        textField.text = string;

        if (nextResponder == nil){
            textField.resignFirstResponder()
        }
        nextResponder?.becomeFirstResponder();
        return false;
    }
    else if ((textField.text?.characters.count)! >= 1  && string.characters.count == 0){
        // on deleting value from Textfield
        let previousTag = textField.tag - 1;

        // get next responder
        var previousResponder = textField.superview?.viewWithTag(previousTag);

        if (previousResponder == nil){
            previousResponder = textField.superview?.viewWithTag(1);
        }
        textField.text = "";
        previousResponder?.becomeFirstResponder();
        return false;
    }
    return true;
}

在 swift 3.0 中转换了 Anurag Soni 的答案

你只需要实现这个方法。

于 2017-06-06T12:31:45.843 回答