This is a code organization question. I have code that will put a gradient background on a view. I want to use this same code on multiple views – therefore, I want to put the code in a function and call it when I configure the views in question.
My question is, where is the correct place to put such a function? I’m very new to iOS development, and I’d like to start out doing things as cleanly and as properly as possible.
As mentioned before, Categories might be the best idea when it comes to DRYing up your code.
Remember that, just for convention, categories in Objective-C are the name of the class you are writing the category for + the addition types.
For example you could name your category files:
UIView+Gradient.h
UIView+Gradient.m
Then you can call it up whenever you want in any subclass if UIView 😀
I believe there is a template for this in Xcode 4.3 (for categories I mean) so go ahead and give it a try.
Remember that you have to write a new method name, otherwise you’d be overwriting an existing method of UIView and that’s not what you want in this case.
Cheers and congrats on your effort to learn proper coding habits from the start 🙂
Kudos to you!