title: Input of UITextField in CollectionViewCell (swift 3 xcode)
body: <p>I have a UITextField that I added within the subview of my collectionViewCell. Here is the code:</p>
  
  <pre><code>class ClientCell: UICollectionViewCell {
  
  var width: CGFloat!
  var height: CGFloat!
  
  var textField: UITextField!
  
  override init(frame: CGRect) {
      super.init(frame: frame)
      width = bounds.width
      height = bounds.height
      setupViews()
  }
  
  func basicTextField(placeHolderString: String) -&gt; UITextField {
      let textField = UITextField()
      textField.font = UIFont.boldSystemFont(ofSize: 12)
      textField.attributedPlaceholder = NSAttributedString(string: placeHolderString, attributes:[NSForegroundColorAttributeName: UIColor.lightGray, NSFontAttributeName: UIFont.boldSystemFont(ofSize: 12)])
      textField.backgroundColor = UIColor.white
      textField.translatesAutoresizingMaskIntoConstraints = false
      return textField
  }
  
  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
  
  func setupViews() {
      backgroundColor = UIColor.white
      layer.addBorder(edge: UIRectEdge.bottom, color: .black, thickness: 0.5)
  
      textField = basicTextField(placeHolderString: "name")
  
      addSubview(textField)
  
  }
  
  func buttonHandler() {
  
      if let textFieldInput = textField.text {
          print (textFieldInput)
      } else {
          print("Nothing in textField")
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I have a button in another class that calls this method, and at the moment prints the current input of the textField (which can be since in the buttonHandler() function). Problem is, the textField is always returning as empty for some reason and I'm not sure why. </p>
  
  <p>EDIT:</p>
  
  <p>this is the function the button calls when pressed (the button and its function are in a separate class to textField):</p>
  
  <pre><code>func testButton() {
      let test = ClientCell()
      test.handler()
  }
  </code></pre>
  
  <p>SOLUTION:</p>
  
  <p>The problem i was having was that I was making a new instance of my collectionViewCell in the class where I wanted the button to be pressed. When the function was called it would be empty. </p>
  
  <p>To solve the problem i used NSNotificationCenter to make a post every time the button was tapped and had observers in the CollectionViewCell class that triggered a function when a post was made. Here is the code.</p>
  
  <p>function called when button is pressed:</p>
  
  <pre><code>func saveData() {
      NotificationCenter.default.post(name: NSNotification.Name("saveProject"), object: nil)
  }
  </code></pre>
  
  <p>code inside viewDidLoad of collectionViewCell:</p>
  
  <pre><code>class ClientCell: UICollectionViewCell {
  
  override init(frame: CGRect) {
      super.init(frame: frame)
      NotificationCenter.default.addObserver(self, selector: #selector(handler), name: NSNotification.Name("saveProject"), object: nil)
  }
  </code></pre>
  
  <p>finally, the function called by the observer in that class</p>
  
  <pre><code>func handler() {
  
      print(textField.text)
  }
  </code></pre>