0

I have a UIViewController which embedded inside another UIViewController. In this child view controller, there are two UITextFields which lets the user to enter some data.

How can I pass that data entered in that embedded view controller back to the parent view controller?

I tried to get a reference to the text fields in the child view controller using this method but I get an error.

let textfield: UITextField = self.childViewControllers.last?.usernameTextField! as UITextField

This usernameTextField is one of the UITextFields inside that embedded UIViewController. I get the following error,

'AnyObject' does not have a member named 'usernameTextField'

Is this the best way to do this or are there any other routes I could take to accomplish this?

Thank you.

Community
  • 1
  • 1
Isuru
  • 30,617
  • 60
  • 187
  • 303

1 Answers1

3

You need to typecast before using it. Check with:

let myObj : MyClass        = self.childViewControllers.last as ? MyClass;
let textfield: UITextField = myObj?.usernameTextField! as UITextField;
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • Thank you! It worked. Although I didn't need that `?` when casting to the `UITextField`. – Isuru Aug 12 '14 at 15:44