gpt4 book ai didi

ios - 在 .xib 所有者的 ViewController 中为 textField 设置委托(delegate)

转载 作者:可可西里 更新时间:2023-11-01 00:37:30 26 4
gpt4 key购买 nike

我正在尝试使用 xib 文件来显示自定义更改 View 。为此,我使用 here 中的 SimpleAlert。我有 .xib 文件,其中包含我的文本字段,作为 .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,现在我想将此 View Controller 添加为它们的委托(delegate)。这样我就可以检查文本字段中输入的内容。

我尝试了不同的方法。首先,我尝试将 textfield.delegate = self 放入 viewDidLoad 方法中,但出现错误“ fatal error :在展开可选值时意外发现 nil”,我认为这是因为文本字段不是创建喷气机。接下来它尝试了 viewDidAppear 方法,结果相同。最后,在我将一个对象添加到 .xib 并将 View Controller 类设置为其类以触发 awakeFromNib 方法之后,我将 textfield.delegate = self 放入该方法中。但我在这里遇到同样的错误。

是否有我没有想到的事情,或者我正在做的事情不会奏效?

问候阿达卡斯

最佳答案

因为您已经声明您的 Controller 符合 UITextFieldDelegate 协议(protocol),所以在 Interface Builder 中执行此操作:

  1. 选择你的文本框
  2. 显示连接检查器
  3. 将有一个 Outlets 部分,其下有 delegate
  4. delegate 右侧的圆圈拖到您的 View Controller 对象中,如下所示

enter image description here

  1. 对每个文本字段执行此操作

现在应该正确调用 textField 委托(delegate)方法。

关于ios - 在 .xib 所有者的 ViewController 中为 textField 设置委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31318880/

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