This looks like another network's question, but I think it fits better on here rather than StackOverflow, since it's not gonna have one basic answer.

We are in the context of iOS development, I'm trying to simplify the use of UITextfield. More specifically, let's say this is an "Amount" textfield when wiring money in a banking app. There is a character limit, but that limit is increased be 2 if there is a ,, that limit is not the same for all currencies, etc etc. Those kinds of simple rules.

Right now, we use the classic delegate/protocol override methods in the controller and type some code there to format and validate the input.

This is not reusable in a way a like, and it's not as clean as I'd like it to be.

Doesn't matter what we have, here's what I'm trying to have, but I can't figure out all of it, and this is where you guys come in.

Ideally, I'd like to be able to write IRule objects and have an extension method on my textfield, so I could mytextfield.AddRule(SomeRule); for each formatting or validation rule I want.

Right now, all I can think of is that my Rule : IRule are extending UITextFieldDelegate, and I do all the overrides I want in those rules objects. then in .AddRule(IRule) I simply set the delegate as the new rule.

This works somewhat fine until I want to add more than one rule, since one object can only have one delegate, which completely defeats the purpose of having different rule objects.

I'd like to have a lot of very simple little rules, like "does not dot character", "allows only negative numbers" etc etc. and add them all to my textfield. Right now, this won't work.

A solution would be a way to still have separate rule objects and add them all together as a bundled rule set and set that as a delegate, but I can't think of how I would do that, programatically.

Another solution would be to make it work to have "multiple" delegates, but that's just not possible, is it?.

Another solution would be to use something else entirely, but I don't know what exists that could help me. Do you?

From all those 3 possible paths I don't know what can be done, and like any good programmer would do, I'd rather ask more experienced people which path is more likely to not cost me 2 weeks more than necessary :)

What would you guys do?

Please feel free to ask any question or clarification you might need. I have 2 weeks to clean and refactor a big chunk of the code base, this is a very rare opportunity for a developer, I'd rather spend it being helped go forward and learn, rather than turned down by moderators.

Thank you very much for your time, I'll be here to edit once I see the comments.


Implement either the Composite pattern or the Decorator pattern.

Composite pattern: Write a class that implements the UITextFieldDelegate protocol and holds an array of other UITextFieldDelegates. For each method, this class loops through its children and calls the method on them.

Decorator pattern: Each of your Rule classes would implement the UITextFieldDelegate protocol and also optionally accept one other Rule. Each rule would do its work and then call the appropriate method on the next rule.

See the design pattern literature for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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