enter image description here

In my iOS project I have a set of custom textfields, e.g. for password entry.

As an example, such textfields can contain additional views and logic - for example, show or hide a password with a press of a button.

Since this logic is not used anywhere outside the textField, I have an idea to create a PasswordTextFieldController and set the textField as its only view.

The show/hide password behavior goes to the PasswordTextFieldController and the controller itself is used as a child view controller with the help of View Controller Containment.

As all the relevant logic has been encapsulated, I could reuse the same PasswordTextFieldController during both signup and login.

Are there any drawbacks in this approach?

1 Answer 1


It's kind of heavy for just one text field in my opinion but it is an option. I think the most common way to do something like this is to make a subclass of UITextField that tracks the extra functionality itself. Even that might be too heavy and it makes composing functionality difficult. I recommend instead that you make your own "PasswordController" class that doesn't derive from UIViewController and has a reference to the text field.

That said, here is an article from someone I respect as a developer who at least entertains the idea: Many Controllers Make Light Work.

  • Thank for your comment. Indeed, it doesn't make any difference if I use a PasswordController derived from NSObject or an UIViewController. For example, a simple object could also be reused in the UICollectionView. But the idea is good and works well for me. Sep 5, 2018 at 9:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.