当用户使用swift键入时,限制UIAlertController中文本字段的输入?

时间:2022-09-25 19:10:06

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.

我找到了关于如何为UIAlertController添加文本字段的示例代码,但我无法限制用户只输入10个字符作为限制,我有一个函数在编辑时更改后删除最后一个字符对于常规文本字段但无法检查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.

1 个解决方案

#1


8  

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

您可以设置警报控制器的文本字段的委托,例如此处显示https://*.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:

请注意,您必须将UITextFieldDelegate协议添加到视图控制器定义中才能进行编译:

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.

然后实现shouldChangeCharactersInRange委托方法,只要文本即将被更改以响应用户交互,就会调用该方法。它可以返回true或false以允许更改或拒绝它。

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:

这将导致输入被忽略,如果它会导致文本字段长度大于10.或者你总是可以将输入截断为10个字符:

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.

第二种方法的缺点是光标总是跳到文本字段的末尾,即使文本插入到其间的某处。

#1


8  

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

您可以设置警报控制器的文本字段的委托,例如此处显示https://*.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:

请注意,您必须将UITextFieldDelegate协议添加到视图控制器定义中才能进行编译:

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.

然后实现shouldChangeCharactersInRange委托方法,只要文本即将被更改以响应用户交互,就会调用该方法。它可以返回true或false以允许更改或拒绝它。

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:

这将导致输入被忽略,如果它会导致文本字段长度大于10.或者你总是可以将输入截断为10个字符:

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.

第二种方法的缺点是光标总是跳到文本字段的末尾,即使文本插入到其间的某处。