1
votes

Im trying to make a login screen similar to the lock screen with 4 textField. The problem i'm facing is with changing the focus from one text field to the next.

  • when i say secondTextField.becomeFirstResponder(), the value of the firstTextField gets copied to the secondTextField
  • i'm using a decimal pad - keyboard type

Here is what i have done so far and i can't find a way around to fix the problem :

  • Using UITextFieldDelegate
  • made IBOutlet connection for all the textField
  • 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(string) - 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()
    }
    

    }

How can i do this correctly, any help and suggestion please.

4
Would be great if i can do these without a library.moz ado

4 Answers

0
votes

Use this delegate method to change the focus

func textFieldShouldReturn(textField: UITextField) -> Bool
0
votes

To add a little more to svrushal answer:

func textFieldShouldReturn(textField: UITextField) -> Bool {
   if textField == firstText {
       secondText.becomeFirstResponder()
   }

   if textField == secondText {
       thirdText.becomeFirstResponder()
   }

   if textField == thirdText {
       fourthText.becomeFirstResponder()
   }

}
0
votes

I found something similar here: similar

Got it to work, with two limitations :

  • Firstly, I cant delete backwards
  • Second, the user has to tap on the first textField if the passCode is wrong. But for now these is good enough.
  • Here are the methods

    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
    }
    
  • method to change the focus :

    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()
    }
    

    }

  • Method to check the 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
    }
    

Hope these help some one later on.