0

I've got a problem at programming for iOS. Already looked for some similar problems but haven't found anything yet.

I'm creating at least 8 custom UIViews. As you can see in the appended code i'm running through a loop creating one instance per round. The reference of every object is on a different space in the memory but when i change a value in one object it only affects the object that has been created in the last loop-round (last created instance). Any Ideas?

PadView.h:

#import <UIKit/UIKit.h>

@interface PadView : UIView {

}

- (void)setText:(NSString*)text;

@end

PadView.m:

#import "PadView.h"
#import "AVFoundation/AVFoundation.h";

@implementation PadView

AVAudioPlayer *player;
UILabel *label;

- (void)setText:(NSString*)text {

    label.text = text;
}

- (void)initialize {

    label = [[ UILabel alloc ] initWithFrame:CGRectMake(0.0, 93.0, 107.0, 13.0)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:9];
    label.textAlignment = UITextAlignmentCenter;
    label.text = @"Empty";
    [self addSubview:label];
    [label release];
}

- (id) initWithCoder:(NSCoder *)aCoder {

    if (self = [super initWithCoder:aCoder]) {

        [self initialize];
    }
    return self;
}

- (id) initWithFrame:(CGRect)rect {

    if (self = [super initWithFrame:rect]) {

        [self initialize];
    }
    return self;
}

- (void)dealloc {

    [super dealloc];
}

@end

Create the Objects:

NSMutableArray *pads = [[NSMutableArray alloc] initWithCapacity:8];

for (int i = 0; i < 8; i++) {

    PadView *pad = [[PadView alloc] initWithFrame:CGRectMake(0.0, i*150, 107.0, 107.0)];
    [padsView addSubview:pad];
    [pads addObject:pad];
}

Call setText:

PadView *pad = [pads objectAtIndex:5];
[pad setText:@"test"];
2
  • it only affects the object that has been created in the last loop-round? or, it also affects the object that has been created in the last loop-round? Commented Feb 7, 2011 at 11:20
  • Sorry to tell you that, with this context, it is difficult to solve your problem. It may be possible if we see your project. Commented Feb 7, 2011 at 12:39

2 Answers 2

2

Your variables:

AVAudioPlayer *player;
UILabel *label;

are defined in the @implementation block, so they are effectively global variables (in the C sense).

So basically, all your instances of PadView will change the same UILabel when you set its text property (which explains the behavior you are seeing).

I'm lacking some context, but it seems that you want label to be an instance variable instead (and I'd assume player as well). If that's the case, you need to declare them in the @interface block as follows:

@interface PadView : UIView {

    AVAudioPlayer *player;
    UILabel *label;

}

- (void)setText:(NSString*)text;

@end
Sign up to request clarification or add additional context in comments.

Comments

0

pads is not initialized

NSMutableArray *pads = [[NSMutableArray alloc] initWithCapacity:8];

and you must release pad after adding as subview and to the array

By convention the class should be named PadView, not padView

edit

for(padView *pad in pads){
    //manipulate each pad 
}



//manipulate a certain pad
padView *pad = [pads objectAtIndex:5];
pad. //...

5 Comments

I already initialize the array in that way. Forgot to mention that sorry. Releasing didn't bring a change though. But what do you mean with initializing pad? InitWithFrame etc. should be an initialization shouldn't it?
or describe more precisely, what u want to do?
Do I need a UIViewController for my custom UIView? Right now I just bequeath (google translator?!) my custom UIView from the UIView class (PadView : UIView )
what I want to do: I'm working on an app with 8 pads, every pad includes an avaudioplayer when i touch a pad the touched-pad should start to play but the app always just plays the last initialised pad, no matter which pad i press
You should add that to the original post, so that everyone will be aware of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.