【问题标题】:Restrict the input of a textfield in UIAlertController when the user types using swift?当用户使用 swift 键入时,限制 UIAlertController 中文本字段的输入?
【发布时间】:2015-03-16 18:20:30
【问题描述】:

我找到了关于如何为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)

谢谢。

【问题讨论】:

    标签: ios swift uialertcontroller


    【解决方案1】:

    您可以将警报控制器的文本字段的 delegate 设置为 例如这里显示https://stackoverflow.com/a/24852979/1187415:

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

    请注意,您必须将UITextFieldDelegate 协议添加到您的 查看控制器定义以进行编译:

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

    然后你实现shouldChangeCharactersInRange委托方法, 每当要更改文本以响应响应时都会调用它 到用户交互。它可以返回truefalse 以允许 改变或否认。

    有各种示例如何限制文本字段的长度 这个委托方法,这里是一种可能的实现:

    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
    }
    

    如果它会导致文本字段,这将导致输入被忽略 长度大于 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
    }
    

    第二种方法的缺点是光标总是跳动 到文本字段的末尾,即使在某处插入了文本 介于两者之间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 2010-11-21
      • 2021-12-28
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多