Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3942344
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:39:06+00:00 2026-05-20T00:39:06+00:00

I’ve got a problem at programming for iOS. Already looked for some similar problems

  • 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"];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:39:06+00:00Added an answer on May 20, 2026 at 12:39 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.