我正在尝试使用xib文件来显示自定义更改视图。为此,我使用here中的SimpleAlert。我有.xib文件,其中包含textFields,并且作为.xib的所有者,我设置了ViewController。
import UIKit
class TestBaumViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var tfFirst: UITextField!
@IBOutlet weak var tfSecond: UITextField!
@IBOutlet weak var tfThird: UITextField!
@IBOutlet weak var tfFourth: UITextField!
init() {
super.init(nibName: "TestBaumViewController", bundle: nil)
initializeTextFields()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//initializeTextFields()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
//initializeTextFields()
}
override func awakeFromNib() {
super.awakeFromNib()
//initializeTextFields()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func initializeTextFields() {
tfFirst.delegate = self // Error Here!
tfFirst.keyboardType = UIKeyboardType.ASCIICapable
tfSecond.delegate = self
tfSecond.keyboardType = UIKeyboardType.NumberPad
tfThird.delegate = self
tfThird.keyboardType = UIKeyboardType.ASCIICapable
tfFourth.delegate = self
tfFourth.keyboardType = UIKeyboardType.NumberPad
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
var result = true
let prospectiveText = (textField.text as NSString).stringByReplacingCharactersInRange(range , withString: string)
if textField == tfFirst {
if count(string) > 0 {
let disallowedCharacterSet = NSCharacterSet(charactersInString: "BW").invertedSet
let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
let resultingStringLengthIsLegal = count(prospectiveText) <= 1
result = replacementStringIsLegal && resultingStringLengthIsLegal
}
}
if textField == tfSecond {
if count(string) > 0 {
let disallowedCharacterSet = NSCharacterSet(charactersInString: "0123456789").invertedSet
let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
let resultingStringLenthIsLegel = count(prospectiveText) <= 3
result = replacementStringIsLegal && resultingStringLenthIsLegel
}
}
if textField == tfThird {
if count(string) > 0 {
let disallowedCharacterSet = NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyz").invertedSet
let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
let resultingStringLenthIsLegel = count(prospectiveText) <= 1
result = replacementStringIsLegal && resultingStringLenthIsLegel
}
}
if textField == tfFourth {
if count(string) > 0 {
let disallowedCharacterSet = NSCharacterSet(charactersInString: "0123456789").invertedSet
let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
let resultingStringLenthIsLegel = count(prospectiveText) <= 3
result = replacementStringIsLegal && resultingStringLenthIsLegel
}
}
return result
}
正如您所看到的,我为我的文本字段添加了IBOutlets,现在我想将此视图控制器添加为其委托。所以我可以查看在文本字段中输入的内容。
我尝试了不同的方法。首先我尝试将textfield.delegate = self
放入viewDidLoad方法,但是我得到一个错误“致命错误:在解开一个Optional值时意外发现nil”我认为这是因为textfield不是jet创建的。接下来,它尝试了viewDidAppear方法,结果相同。最后,在我将一个Object添加到.xib并将视图控制器类设置为其类以使awakeFromNib方法触发之后,我将textfield.delegate = self
放入该方法中。但我在这里得到了同样的错误。
我有没有想到的事情,或者我正在做一些不起作用的事情?
尊重阿达卡斯
答案 0 :(得分:4)
由于您已经声明您的控制器符合UITextFieldDelegate
协议,请在Interface Builder中执行此操作:
Outlets
部分,其中包含delegate
。delegate
右侧的圆圈拖动到View Controller对象,如
现在应该正确调用textField委托方法。
答案 1 :(得分:1)
didSet怎么样?
class TestBaumViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var tfFirst: UITextField! {
didSet {
tfFirst.delegate = self
}
}
...
或者您也可以在XIB中拖动文本字段委托。 右键单击文本字段并将delegate属性拖到视图控制器。