I’m making a subclass of UIView completely in code (no IB), let’s call it ContentView. In this view I’ve already set up several players for sounds and video, as well as several imageViews (nothing special).
Next, I was planning to subclass ContentView several times in order to load different media for each view. All of these views would have the same view controller since the interface would be the same for all of them, only the content (sounds, video and images) would change.
So my approach to this problem was to declare several NSString *const in ContentView.h and specify their keys/values in the implementation file of each subclass view of ContentView, in the form of static NSString *const, since I would be reusing them to load different media for each view and did not want them in the global name space.
Here is some mockup code that illustrates what I’m talking about:
In ContentView.h
@interface ContentView : UIView {
NSString *const kSoundFile1;
NSString *const kSoundFile2;
NSString *const kSoundFileType;
NSString *const kMovieFile1;
NSString *const kMovieFile2;
NSString *const kMovieFileType;
NSString *const kImage1;
NSString *const kImage2;
and in ContentView.m, something of the sort,
@implementation ContentView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSString *filePath1 = [[NSBundle mainBundle] pathForResource: kSoundFile1
ofType: kSoundFileType;
NSURL *url1 = [[NSURL alloc] initFileURLWithPath:filePath1];
AVAudioPlayer *audioPlayer1 = [AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audioPlayer1 prepareToPlay];
[url1 release];
… and so on, for the rest of the sound files and movies (except for the images, for which i’m using imageNamed:).
Then on the implementation file of each subclass of ContentView I have just this:
@implementation ContentViewSubclass
static NSString *const kSoundFile1 = @"sound1";
static NSString *const kSoundFile2 = @"sound2";
static NSString *const kSoundFileType = @"wav";
static NSString *const kMovieFile1 = @"movie1";
static NSString *const kMovieFile2 = @"movie2";
static NSString *const kMovieFileType = @"mov";
static NSString *const kImage1 = @"image1.png";
static NSString *const kImage2 = @"image2.png";
@end
I can’t make this work. There are no compiler errors or warnings, simply nothing plays or shows. Am I doing something wrong, or is this just not the right approach to the problem?
I would really appreciate some insights. Thanks in advance.
Although the answer below from @deanWombourne makes perfect sense, I had an issue with his solution. But I have found out what was wrong with it (at least this is my take on it and it’s working now).
A UIView subclass already has its own designated initializer, which is
-(id)initWithFrame, so calling-(id)initon any subsequent subclass of ContentView is not going to update any instance variables since[super init]directs to nowhere (or better, first the superclass runsinitWithFrameand only then it runsinit, which is the same as having done nothing).So my solution is the following:
(after changing the ivars on ContentView.h to the form of
NSString *pointer, for example,NSString *kSoundFile1),First update the strings on the designated initializer
-(id)initWithFrame, and only then call super.It works just fine now.
My sincere thanks to @deanWombourne for the help given in solving this problem.