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.

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It worked. Although I didn't need that ? when casting to the UITextField.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.