FONSPA
FONSPA

Reputation: 13

Swift: How to use two func textfield at same time

I'm using Xcode 8 and Swift 3.

I have a project with 3 textfields, 1 button to clear and label to display result.

Inside my class ViewController I have:

class ViewController: UIViewController,UITextFieldDelegate {

@IBOutlet weak var input1: UITextField!
@IBOutlet weak var input2: UITextField!
@IBOutlet weak var input3: UITextField!
@IBOutlet weak var lblResult: UILabel!
@IBOutlet weak var clearButton: UIButton!

I want to limit my textfields inputs to max 3 digits but also to a value of 360. I manage to get code for both things and they work if used only one at a time but because they both start with func textfield I can't make them both work together. Do I have to do it in different class? I know this is a basic question but its part of the learning process.

These are the two codes I want to combine:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, 
               replacementString string: String) -> Bool
{
    let maxLength = 3
    let currentString: NSString = textField.text! as NSString
    let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString
    return newString.length <= maxLength
}

and:

func textField(_ textField: UITextField,
               shouldChangeCharactersIn range: NSRange,
               replacementString string: String) -> Bool
{
    var startString = ""
    if (textField.text != nil)
    {
        startString += textField.text!
    }
    startString += string
    let limitNumber = Int(startString)
    if limitNumber! > 360
    {
        return false
    }
    else
    {
        return true;
    }
}

They are both inside the class ViewController.

Thanks for the help!

Upvotes: 1

Views: 1774

Answers (4)

Acrasia
Acrasia

Reputation: 201

People didn't understood the question. Actually, all you need is merging your statement as mentioned by javimuu.

  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange,
               replacementString string: String) -> Bool
{
    let maxLength = 3
    let currentString: NSString = textField.text! as NSString
    let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString
    
    var startString = ""
    if (textField.text != nil)
    {
        startString += textField.text!
    }
    startString += string
    let limitNumber = Int(startString)
 

    return newString.length <= maxLength && limitNumber! <= 360
}

Upvotes: 0

javimuu
javimuu

Reputation: 1859

If I am not mistaken, here is all you need:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, 
               replacementString string: String) -> Bool {
    let maxLength = 3
    let limitValue = 360

    let text = textField.text!
    let currentString: NSString = text as NSString
    let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString

   var startString = ""

    if !text.isEmpty {
        startString += text
    }
    startString += string

    let limitNumber = Int(startString)!

    return limitNumber < limitValue && newString.length <= maxLength
}

Update:

Auto focus on a next texfield.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, 
                   replacementString string: String) -> Bool {
    let maxLength = 3
    let limitValue = 360

    let text = textField.text!
    let currentString: NSString = text as NSString
    let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString

   var startString = ""

    if !text.isEmpty {
        startString += text
    }
    startString += string

    let limitNumber = Int(startString)!

    let newLength: Int = newString.length

    if textField == input1 {
        if newLength == maxLength {
            input2.becomeFirstResponder()
        }
    }
    if textField == input2 {
        if newLength == maxLength {
            input3.becomeFirstResponder()
        }
    }

    if textField == input3 {
        if newLength ==  maxLength {
           self.view.endEditing(true)
        }
    }

    return limitNumber < limitValue && newLength <= maxLength
}

Upvotes: 1

MjZac
MjZac

Reputation: 3526

Swift switch statement will do it.

func textField(_ textField: UITextField,
               shouldChangeCharactersIn range: NSRange,
               replacementString string: String) -> Bool
{
    switch textField {
    case input1:
        // ...
    case input2:
        // ...
    case input3:
        // ...
    default:
        break
    }
}

Upvotes: 1

anho
anho

Reputation: 1735

You need to add an if statement in func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool checking for the current textFieldlike this:

func textField(_ textField: UITextField,  shouldChangeCharactersIn range: NSRange,  replacementString string: String) -> Bool {
   if textFiled == input1 {
   // do logic for input1
   } else if textFiled == input2 {
   // do logic for input2
   }
}

Upvotes: 1

Related Questions