Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict the input of a textfield in UIAlertController when the user types using swift?

I have found this sample code about how to add the textfield for the UIAlertController but I am unable to restrict the the users to enter only 10 characters as a limit, I have a function to remove the last character when it is go over when editing changed for a regular textfield but unable to check the textfield on the UIAlertController.

//Function to Remove the last character

func trimStr(existStr:String)->String{

    var countExistTextChar = countElements(existStr)

    if countExistTextChar > 10 {
        var newStr = ""
        newStr = existStr.substringToIndex(advance(existStr.startIndex,(countExistTextChar-1)))
        return newStr

    }else{
        return existStr
    }

}

 var inputTextField: UITextField?

    //Create the AlertController
    let actionSheetController: UIAlertController = UIAlertController(title: "Alert", message: "Swiftly Now! Choose an option!", preferredStyle: .Alert)

    //Create and add the Cancel action
    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
        //Do some stuff
    }
    actionSheetController.addAction(cancelAction)
    //Create and an option action
    let nextAction: UIAlertAction = UIAlertAction(title: "Next", style: .Default) { action -> Void in


    }
    actionSheetController.addAction(nextAction)
    //Add a text field
    **actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
        //TextField configuration
        textField.textColor = UIColor.blueColor()

        inputTextField = textField**


    }

    //Present the AlertController
    self.presentViewController(actionSheetController, animated: true, completion: nil)

Thank you.

like image 312
user2449557 Avatar asked Jan 18 '15 17:01

user2449557


1 Answers

You can set the delegate of the alert controller's text field as for example showed here https://stackoverflow.com/a/24852979/1187415:

actionSheetController.addTextFieldWithConfigurationHandler { [weak self] textField -> Void in
    //TextField configuration
    textField.textColor = UIColor.blueColor()
    textField.delegate = self
}

Note that you have to add the UITextFieldDelegate protocol to your view controller definition to make that compile:

class ViewController: UIViewController, UITextFieldDelegate {
    // ...

Then you implement the shouldChangeCharactersInRange delegate method, which is called whenever the text is about to be changed in response to user interaction. It can return true or false to allow the change or to deny it.

There are various examples how to limit the text field length in this delegate method, here is one possible implementation:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange,
    replacementString string: String) -> Bool {
        let newString = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string) as NSString
        return newString.length <= 10
}

This will cause input to be ignored if it would cause the text field length to be greater than 10. Alternatively you could always truncate the input to 10 characters:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange,
    replacementString string: String) -> Bool {
        let newString = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
        textField.text = trimStr(newString)
        return false
}

A disadvantage of the second method is that the cursor always jumps to the end of the text field, even if text is inserted somewhere in between.

like image 85
Martin R Avatar answered Oct 12 '22 21:10

Martin R