问题:
文字字段不接受字符。
shouldChangeCharactersInRange
没有为所有3个文本字段调用。
我实现了3个委托类,就像main
一样。完全实现所有方法,因为委托方法在main
中实现。在运行时调用除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;
}
}
答案 0 :(得分:0)
在Swift 3.0中更改文本字段编辑方法。 请使用以下附加方法
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
}