开发者

Issue with multiple instances of same UIView object

开发者 https://www.devze.com 2023-02-09 04:18 出处:网络
I\'ve got a problem at programming for iOS. Already looked for some similar problems but haven\'t found anything yet.

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:

#i开发者_Python百科mport "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"];


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


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. //...
0

精彩评论

暂无评论...
验证码 换一张
取 消