title: Set delegate for textField in ViewController that is owner of .xib
body: <p>I am trying to use an xib file to display a custom alter view. For that purpose i use the SimpleAlert from <a href="https://www.cocoacontrols.com/controls/simplealert" rel="nofollow noreferrer">here</a>. I have the .xib file with my textFields in it and as the owner of the .xib i set the ViewController is created.</p>
  <pre><code>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: &quot;TestBaumViewController&quot;, 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) -&gt; Bool {
      var result = true
      let prospectiveText = (textField.text as NSString).stringByReplacingCharactersInRange(range , withString: string)
      
      if textField == tfFirst {
          if count(string) &gt; 0 {
              let disallowedCharacterSet = NSCharacterSet(charactersInString: &quot;BW&quot;).invertedSet
              let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
          
              let resultingStringLengthIsLegal = count(prospectiveText) &lt;= 1
          
              result = replacementStringIsLegal &amp;&amp; resultingStringLengthIsLegal
          }
      }
      
      if textField == tfSecond {
          if count(string) &gt; 0 {
              let disallowedCharacterSet = NSCharacterSet(charactersInString: &quot;0123456789&quot;).invertedSet
              let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
              
              let resultingStringLenthIsLegel = count(prospectiveText) &lt;= 3
              
              result = replacementStringIsLegal &amp;&amp; resultingStringLenthIsLegel
          }
      }
      
      if textField == tfThird {
          if count(string) &gt; 0 {
              let disallowedCharacterSet = NSCharacterSet(charactersInString: &quot;abcdefghijklmnopqrstuvwxyz&quot;).invertedSet
              let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
              
              let resultingStringLenthIsLegel = count(prospectiveText) &lt;= 1
              
              result = replacementStringIsLegal &amp;&amp; resultingStringLenthIsLegel
          }
      }
      
      if textField == tfFourth {
          if count(string) &gt; 0 {
              let disallowedCharacterSet = NSCharacterSet(charactersInString: &quot;0123456789&quot;).invertedSet
              let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
              
              let resultingStringLenthIsLegel = count(prospectiveText) &lt;= 3
              
              result = replacementStringIsLegal &amp;&amp; resultingStringLenthIsLegel
          }
      }
      return result
  }
  </code></pre>
  <p>As you can see i added IBOutlets for my textfields and now i wanted to add this view controller as their delegate. So i can check what was entered in the textfields.</p>
  <p>I tried different approaches. First i tried putting the <code>textfield.delegate = self</code> into the viewDidLoad method but there i get an error &quot;fatal error: unexpectedly found nil while unwrapping an Optional value&quot; i think that is because the textfield aren't created jet. Next it tried the viewDidAppear method, with the same result. And finally, after i added an Object to the .xib with the view controller class set as its class to get the awakeFromNib method to fire, i put the <code>textfield.delegate = self</code> into that method. But i get the same error here.</p>
  <p>Is there something i did not think of or am i doing something that will not be working?</p>
  <p>regards Adarkas</p>