English 中文(简体)
利用NSString *const
原标题:Loading different media files for each view subclass using NSString *const

I m在代码(无一B)中填满一子,请打电话ContentView。 在这方面,我已经为声音和视频以及一些图像评论(无专业)建立了几个参与者。

其次,我计划分级ContentView,以便把不同的媒体装上每种观点。 所有这些观点都有相同的观点控制者,因为对所有这些观点的接口相同,只有内容(声音、录像和图像)才会改变。

因此,我处理这一问题的方法是,在ContentView上,以的形式,宣布, 并具体说明其在执行档案中对每一次观点的关键/价值,其形式为,即static NSString *const,因为我将重新加以利用,为每一种观点装上不同的媒体,而不要将其用全球名称。

这里有一些模拟法,表明了我所说的话:

内容提要

@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;

内容提要 观点,例如,

@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:).

然后,在<>ContentView每一次下级的执行档案上公布。 我只是这样:

@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

我不能做这项工作。 没有任何编辑错误或警告,根本就没有作用或显示。 我做的是错的,或者说,这不是正确对待问题的办法?

我确实会赞赏一些见解。 提前感谢。

最佳回答

www.un.org/Depts/DGACM/index_spanish.htm 虽然下文对“deanWombourne”的答复是完美的,但我对他的解决办法有问题。 但是,我发现其中的错误之处(至少这是我对它的看法,现在它一直在工作)。

A UIView subclass already has its own designated initializer, which is -(id)initWithFrame, so calling -(id)init on any subsequent subclass of ContentView is not going to update any instance variables since [super init] directs to nowhere (or better, first the superclass runs initWithFrame and only then it runs init, which is the same as having done nothing).

因此,我的解决办法如下:

(在将关于内容的第ivars改为之后) 缩略语

@implementation ContentViewSubclass

- (id)initWithFrame:(CGRect)frame {

        kSoundFile1 = @"sound1";
        kSoundFile2 = @"sound2";
        kSoundFileType = @"wav";
        kMovieFile1 = @"movie1";
        kMovieFile2 = @"movie2";
        kMovieFileType = @"mov";
        kImage1 = @"image1.png";
        kImage2 = @"image2.png";

    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}

@end

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.

问题回答

我认为,我也许会这样做,忽略了我的其他答案:

页: 1 开始采用<代码>nil

然后,在每一次级别上,你创建<>新<>/>。 仅用同一名称加以说明(静态关键词确实如此!) 在《刑法》中,他们有相同的名字,并不意味着他们重新提到同一个目标:

你的文稿可以看看看新的雕像,它只是使用你档案中的定义,而从来,这些档案从未被打上过任何东西,因此,我仍要打上<条码>。

你们需要界定你等子类的扼杀:

@implementation ContentViewSubclass

- (id)init {
    self = [super init];
    if (self) {
        kSoundFile1 = @"sound1";
        kSoundFile2 = @"sound2";
        kSoundFileType = @"wav";
        kMovieFile1 = @"movie1";
        kMovieFile2 = @"movie2";
        kMovieFileType = @"mov";
        kImage1 = @"image1.png";
        kImage2 = @"image2.png";
    }
    return self;
}

@end

PS “k, in front of these name don t dos nomore - I ded.





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...