Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading different media files for each view subclass using NSString *const
    primarykey
    data
    text
    <p>I'm making a subclass of UIView completely in code (no IB), let's call it <strong>ContentView</strong>. In this view I've already set up several players for sounds and video, as well as several imageViews (nothing special).</p> <p>Next, I was planning to subclass <strong>ContentView</strong> 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.</p> <p>So my approach to this problem was to declare several <code>NSString *const</code> in <strong>ContentView</strong>.h and specify their keys/values in the implementation file of each subclass view of <strong>ContentView</strong>, in the form of <code>static NSString *const</code>, since I would be reusing them to load different media for each view and did not want them in the global name space.</p> <p>Here is some mockup code that illustrates what I'm talking about:</p> <p>In ContentView.h</p> <pre><code>@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; </code></pre> <p>and in ContentView.m, something of the sort,</p> <pre><code>@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]; </code></pre> <p>... and so on, for the rest of the sound files and movies (except for the images, for which i'm using <code>imageNamed:</code>).</p> <p>Then on the implementation file of each subclass of <strong>ContentView</strong> I have just this:</p> <pre><code>@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 </code></pre> <p>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?</p> <p>I would really appreciate some insights. Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload