Ios 为.xib所有者的ViewController中的textField设置委托

Ios 为.xib所有者的ViewController中的textField设置委托,ios,xcode,swift,xib,nib,Ios,Xcode,Swift,Xib,Nib,我正在尝试使用xib文件来显示自定义的alter视图。为此,我使用了中的simplelert。我有一个.xib文件,其中有我的文本字段,作为.xib文件的所有者,我设置了ViewController的创建 import UIKit class TestBaumViewController: UIViewController, UITextFieldDelegate { @IBOutlet weak var tfFirst: UITextField! @IBOutlet weak var tf

我正在尝试使用xib文件来显示自定义的alter视图。为此,我使用了中的simplelert。我有一个.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
}
导入UIKit
类TestBaumViewController:UIViewController、UIExtFieldDelegate{
@IBOutlet弱变量tfFirst:UITextField!
@IBOutlet弱var tfSecond:UITextField!
@IBOutlet弱var tfThird:UITextField!
@第四个:UITextField!
init(){
super.init(nibName:“TestBaumViewController”,bundle:nil)
initializeTextFields()
}
必需的初始化(编码器aDecoder:NSCoder){
super.init(编码者:aDecoder)
}
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
//initializeTextFields()
}
覆盖功能视图显示(动画:Bool){
super.viewdide显示(动画)
//initializeTextFields()
}
重写func awakeFromNib(){
super.awakeFromNib()
//initializeTextFields()
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
func initializeTextFields(){
tfFirst.delegate=self//此处出错!
tfFirst.keyboardType=UIKeyboardType.ascicapable
tfSecond.delegate=self
tfSecond.keyboardType=UIKeyboardType.NumberPad
tfThird.delegate=self
tfThird.keyboardType=UIKeyboardType.ascicable
tfFourth.delegate=self
tfFourth.keyboardType=UIKeyboardType.NumberPad
}
func textField(textField:UITextField,shouldChangeCharactersRange:NSRange,replacementString:string)->Bool{
var结果=真
让prospectiveText=(textField.text作为NSString)。StringByReplacingCharactersRange(range,with字符串:string)
如果textField==tfFirst{
如果计数(字符串)>0{
设disallowedCharacterSet=NSCharacterSet(charactersInString:“BW”)。invertedSet
让replacementStringIsLegal=string.rangeOfCharacterFromSet(disallowedCharacterSet)==nil
让resultingStringLengthIsLegal=计数(prospectiveText)0{
设disallowedCharacterSet=NSCharacterSet(charactersInString:“0123456789”)。invertedSet
让replacementStringIsLegal=string.rangeOfCharacterFromSet(disallowedCharacterSet)==nil
让resultingStringLenthIsLegel=计数(prospectiveText)0{
设disallowedCharacterSet=NSCharacterSet(charactersInString:“abcdefghijklmnopqrstuvwxyz”).invertedSet
让replacementStringIsLegal=string.rangeOfCharacterFromSet(disallowedCharacterSet)==nil
让resultingStringLenthIsLegel=计数(prospectiveText)0{
设disallowedCharacterSet=NSCharacterSet(charactersInString:“0123456789”)。invertedSet
让replacementStringIsLegal=string.rangeOfCharacterFromSet(disallowedCharacterSet)==nil
让resultingStringLenthIsLegel=count(prospectiveText)那么didSet呢

class TestBaumViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var tfFirst: UITextField! {
        didSet {
            tfFirst.delegate = self
        }
    }
...
或者也可以拖动XIB中的文本字段。
右键单击文本字段并将委派属性拖动到视图控制器。

由于您已声明控制器符合
UITextFieldDelegate
协议,请在界面生成器中执行以下操作:

  • 选择您的文本字段
  • 显示连接检查器
  • 将有一个
    Outlets
    部分,下面有
    delegate
  • 委托
    右侧的圆圈拖动到视图控制器对象上,如下所示
  • 对每个文本字段执行此操作

  • 现在应该正确调用textField委托方法。

    您不能在interface Builder中设置委托吗?我尝试过,但在视图控制器代码上出现蓝线时,没有得到创建委托的提示。首先,我无法重现您的问题。您的代码工作正常。其次,在xib文件中,选择“文件的所有者”然后在Xcode的右角选择“Show the identity inspector”,然后在类字段中输入xibok的类名,如果代码工作正常,那么问题可能来自于我使用的SimpleAlert类。我已经像您所描述的那样设置了文件的所有者。在界面生成器中选择一个文本字段,然后按
    Opt Cmd 6
    (连接检查员)。从Outlets>delegate拖动到视图控制器场景中的视图控制器对象。这是否有效?
    didSet
    在初始化期间从不调用:在进行委派之前在初始化器中设置属性时,不会调用willSet和didSet观察者。这不是初始化。在这种情况下,可以尝试调用didSet in我不再出现第一个错误了。但只要我点击一个文本字段,我就会在AppDelegate.swift文件中发现一个异常。你能复制/粘贴崩溃日志吗?只要我知道我将如何做;)。