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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
scroll top