gpt4 book ai didi

ios - 文本字段 shouldChangeCharactersInRange 未调用

转载 作者:行者123 更新时间:2023-11-30 14:02:37 26 4
gpt4 key购买 nike

问题:

文本字段不接受字符。

shouldChangeCharactersInRange 未针对所有 3 个文本字段调用。

我有 3 个委托(delegate)类的实现,就像在 main 中一样。所有方法的实现与 main 中实现的委托(delegate)方法完全相同。除 shouldChangeCharactersInRange 之外的所有其他方法均在运行时调用。编辑框根本不接受任何字符。

import UIKit

class ViewController: UIViewController , UITextFieldDelegate{

@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
@IBOutlet weak var textField3: UITextField!

var t1Delegate : text1Delegate!
var t2Delegate : text2Delegate!
var t3Delegate : text3Delegate!

override func viewDidLoad() {
t2Delegate = text2Delegate()
textField2.delegate = t2Delegate
}
override func viewDidAppear(animated: Bool) {
textField1.delegate = self
textField1.becomeFirstResponder()
t3Delegate = text3Delegate()
textField3.delegate = t3Delegate
super.viewDidLoad()
}
func textField (textField : UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String ) -> Bool {
print("*. Should change text.")
return true;
}
// UITextField Delegates
func textFieldDidBeginEditing(textField: UITextField) {
print("*. TextField did begin editing method called")
}
func textFieldDidEndEditing(textField: UITextField) {
print("*. TextField did end editing method called")
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
print("*. TextField should begin editing method called")
return true;
}
func textFieldShouldClear(textField: UITextField) -> Bool {
print("*. TextField should clear method called")
return true;
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
print("*. TextField should end editing method called")
return true;
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
print("*TextField should return method called")
textField.resignFirstResponder();
return true;
}
}

最佳答案

在 Swift 3.0 中更改 Textfield 编辑方法。请配合下面附上的方法使用

class PromoCodeViewController: UIViewController , UITextFieldDelegate{
}

override func viewDidLoad() {
super.viewDidLoad()

tf_GiftVoucher.delegate = self

// Do any additional setup after loading the view.
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range:NSRange, replacementString string: String) -> Bool {
if tf_GiftVoucher == textField {
let text = textField.text
if (text?.characters.count)! > 15 || string == " " {
return false
}
}
return true
}

关于ios - 文本字段 shouldChangeCharactersInRange 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32802918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com