【发布时间】:2015-09-26 23:06:04
【问题描述】:
问题:
文本字段不接受字符。
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;
}
}
【问题讨论】:
-
根据您提供的代码,您代码中的
shouldChangeCharactersInRange委托方法只会为textField1调用。 -
你连接了 IBOutlets 了吗??
-
是的,每个 IBOutlet(从声明行左侧的小点开始)都连接到情节提要中的每个文本字段。
-
对rmaddy来说,我代码中的委托方法只连接到textField1。其他文本字段分别连接到用户定义的委托类 text2Delegate 和 text3Delegate。这些委托类的实现与 main 中的委托方法一样(纯打印输出)
标签: ios swift uitextfield swift2