This might be a stupid question, but I’ve found myself getting tired of declaring same property over and over multiple view controllers. Is there a better design to do something simple as below:
Consider this:
I have a SoundManager class that is used throughout my iphone project. This class simply plays audio (click sound) when a user presses button.
Now, I have been doing this:
ViewController A: .h
SoundManager *mgr;
@property (nonatomic,retain) SoundManager *mgr;
ViewController A: .m
@synthesize *mgr;
and in viewDidLoad
if (mgr == nil)
mgr = [[SoundManager alloc] init];
Then I repeat this over all my view controllers. This is cumbersome to say the least. There must be a better way of doing something like this – or at least a code generator utility that I could use?
Anyone have any suggestions?
You could implement a singleton for that class:
Then call [SoundManager sharedManager] wherever you need a SoundManager instance.