Xcode ios限制UIAlertView Textview中的字符

时间:2017-01-21 23:05:04

标签: ios xcode uitextview

我已经看过很多这方面的教程,但它们是针对Objective C的。我想在我的应用中实现这个功能,但不知道该怎么做。 任何帮助都很棒!

1 个答案:

答案 0 :(得分:1)

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let alert = UIAlertView(title: "Client id", message: "some title", delegate: self, cancelButtonTitle: "ok")
     alert.alertViewStyle = .plainTextInput

    let textField: UITextField = alert.textField(at: 0)!
    textField.delegate = self

    alert.show()
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if (textField.text?.characters.count)! < 10
    {
        return true
    }
    return false
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

https://i.stack.imgur.com/bDNSk.png