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?

up vote 0 down vote accepted

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. – Richard Topchiy Sep 5 at 9:55

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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