如何在自定义中多次使用函数 Class
How to use functions more than once in a Custom Class
有没有实施
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {}
两次?我正在尝试创建一个具有多个视图控制器的转换应用程序,其中所有视图控制器都具有相似的 UITextFields
。我遵循了关于 www.globalnerdy.com 的精彩教程,该教程展示了如何创建自定义 class 来限制字符并仅允许给定文本字段中的某些字符。代码完成后,您可以在 Attributes Inspector 和 View Controller 中修改属性。下面是我创建的 MaxLengthTextField
自定义 class。
class MaxLengthTextField: UITextField, UITextFieldDelegate {
private var characterLimit: Int?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
delegate = self
}
@IBInspectable var maxLength: Int {
get {
guard let length = characterLimit else {
return Int.max
}
return length
}
set {
characterLimit = newValue
}
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard string.characters.count > 0 else {
return true
}
let currentText = textField.text ?? ""
let prospectiveText = (currentText as NSString).replacingCharacters(in: range, with: string)
return allowedIntoTextField(text: prospectiveText)
}
func allowedIntoTextField(text: String) -> Bool {
return text.characters.count <= maxLength
}
}
这里是 AllowedCharacters 自定义 class,MaxLengthTextField 的子class。
class AllowedCharsTextField: MaxLengthTextField {
@IBInspectable var allowedChars: String = ""
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
delegate = self
autocorrectionType = .no
}
override func allowedIntoTextField(text: String) -> Bool {
return super.allowedIntoTextField(text: text) && text.containsOnlyCharactersIn(matchCharacters: allowedChars)
}
}
private extension String {
// Returns true if the string contains only characters found in mainCharacters.
func containsOnlyCharactersIn(matchCharacters: String) -> Bool {
let disallowedCharacterSet = CharacterSet(charactersIn: matchCharacters).inverted
return self.rangeOfCharacter(from: disallowedCharacterSet) == nil
}
}
我遇到的问题是限制小数点“.”。在我的代码中。我已经尝试使用我在 Stack Overflow 上找到的代码(有效)来实现 func textField(_ textField: UITextField, shouldChangeCharactersIn...
函数两次以限制小数,但由于特定文本字段已经是限制字符的自定义 class,我我无法使文本字段成为视图控制器的委托并同时限制 "dots"。
任何指导、参考和直接帮助将不胜感激!
在您的代码中,方法由它们的签名唯一标识 - 或者方法名称加上它采用的参数(参数类型)的组合。因此,您不能在代码中两次使用完全相同的方法。这将导致编译器错误。
与其执行两次 textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {}
方法,不如查看传入的 textField
参数以确定首先发送委托方法的文本字段。
将您的视图控制器设为 UITextFieldDelegate
,然后在您的视图控制器中为您的不同文本字段设置插座。因此,如果您的文本字段是 text1
和 text2
,您的委托方法将如下所示:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == text1 {
// Handle text1 code
} else if textField == text2 {
// Handle text2 code
}
}
希望以上内容对您有意义,但如果没有,请随时要求澄清 :)
有没有实施
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {}
两次?我正在尝试创建一个具有多个视图控制器的转换应用程序,其中所有视图控制器都具有相似的 UITextFields
。我遵循了关于 www.globalnerdy.com 的精彩教程,该教程展示了如何创建自定义 class 来限制字符并仅允许给定文本字段中的某些字符。代码完成后,您可以在 Attributes Inspector 和 View Controller 中修改属性。下面是我创建的 MaxLengthTextField
自定义 class。
class MaxLengthTextField: UITextField, UITextFieldDelegate {
private var characterLimit: Int?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
delegate = self
}
@IBInspectable var maxLength: Int {
get {
guard let length = characterLimit else {
return Int.max
}
return length
}
set {
characterLimit = newValue
}
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard string.characters.count > 0 else {
return true
}
let currentText = textField.text ?? ""
let prospectiveText = (currentText as NSString).replacingCharacters(in: range, with: string)
return allowedIntoTextField(text: prospectiveText)
}
func allowedIntoTextField(text: String) -> Bool {
return text.characters.count <= maxLength
}
}
这里是 AllowedCharacters 自定义 class,MaxLengthTextField 的子class。
class AllowedCharsTextField: MaxLengthTextField {
@IBInspectable var allowedChars: String = ""
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
delegate = self
autocorrectionType = .no
}
override func allowedIntoTextField(text: String) -> Bool {
return super.allowedIntoTextField(text: text) && text.containsOnlyCharactersIn(matchCharacters: allowedChars)
}
}
private extension String {
// Returns true if the string contains only characters found in mainCharacters.
func containsOnlyCharactersIn(matchCharacters: String) -> Bool {
let disallowedCharacterSet = CharacterSet(charactersIn: matchCharacters).inverted
return self.rangeOfCharacter(from: disallowedCharacterSet) == nil
}
}
我遇到的问题是限制小数点“.”。在我的代码中。我已经尝试使用我在 Stack Overflow 上找到的代码(有效)来实现 func textField(_ textField: UITextField, shouldChangeCharactersIn...
函数两次以限制小数,但由于特定文本字段已经是限制字符的自定义 class,我我无法使文本字段成为视图控制器的委托并同时限制 "dots"。
任何指导、参考和直接帮助将不胜感激!
在您的代码中,方法由它们的签名唯一标识 - 或者方法名称加上它采用的参数(参数类型)的组合。因此,您不能在代码中两次使用完全相同的方法。这将导致编译器错误。
与其执行两次 textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {}
方法,不如查看传入的 textField
参数以确定首先发送委托方法的文本字段。
将您的视图控制器设为 UITextFieldDelegate
,然后在您的视图控制器中为您的不同文本字段设置插座。因此,如果您的文本字段是 text1
和 text2
,您的委托方法将如下所示:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == text1 {
// Handle text1 code
} else if textField == text2 {
// Handle text2 code
}
}
希望以上内容对您有意义,但如果没有,请随时要求澄清 :)