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

标签 ios swift 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)

谢谢。

最佳答案

您可以将警报 Controller 文本字段的委托(delegate)设置为 例如这里显示 https://stackoverflow.com/a/24852979/1187415 :

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

请注意,您必须将 UITextFieldDelegate 协议(protocol)添加到您的 查看 Controller 定义以进行编译:

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

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

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

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
}

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

关于ios - 当用户使用 swift 键入时,限制 UIAlertController 中文本字段的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28012867/

相关文章:

ios - 如何通过按钮和点击外部正确关闭 UIAlertController?

ios - 调用 UITableView scrollEnabled 问题

ios - 相机参数 - iPhone 7

ios - 带有 CollectionViewCell 和 UILongPressGesture 错误的 XIB

ios - 在 UIAlertController 中将图像添加到 UIAlertAction

ios - UIAlertController 阻止 segue 执行 ("window is not equal to view’ s 窗口“)

ios - 你如何从 UITableViewCell 执行 segue?

ios - swift3中的编译错误:“AnyObject”不是“NSObject”的子类型

ios - 跳过一个通知,每天重复

ios - 在堆栈上嵌入 2 个或更多导航 Controller