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

  • Home
  • SEARCH
  • 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 8497945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:16:08+00:00 2026-06-11T00:16:08+00:00

How do I create multiple objects with the same code, but each with a

  • 0

How do I create multiple objects with the same code, but each with a unique name?

If I create an object in objective C something like:

MyClass *myObject = [[MyClass alloc] init];

I have an object named myObject that I can refer to, to do stuff like:

[myObject setBackgroundColor:[UIColor blueColor]];

But what if I want to create multiple objects with the same allocation code? For instance if I am using this code in an action so that every time a button is tapped a new object is created. Then I would have multiple objects named myObject. SO when I used the code to set the background color it would turn all of myObject’s background color blue.

If each object created had a unique name this would not be an issue, but how to give each object created with the same code a unique name?

One way would be to rename the objects after creation and attach a number increment, but how can I rename an object? Or maybe I could set each object created with a unique name in the first place, but how can I insert a unique “string” for the name of each new object?

Thanks for reading!

UPDATE!!

I created an “init with tag method”, but each time I create a new object the previously created object also receives the new tag, so each object has the same tag, not unique tags. Any help?

The init method:

- (id) initWithTag:(int) theTag
{
    self = [self init];
    if (self)
    {
        self.tag = theTag;
    }
    return self;
}

The object creation code:

MyClass *myObject = [[MyClass alloc] initWithTag:tagCount];
tagCount ++;
  • 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-06-11T00:16:10+00:00Added an answer on June 11, 2026 at 12:16 am

    I had the same request, longer time ago.

    I prefer to do it that way:

    @implementation testViewController {
        NSMutableArray *myarray;
    }
    
    - (id)init
    {
        self = [super init];
        if (self) {
            myarray = [NSMutableArray array];
        }
        return self;
    }
    
    - (void) buttonClickMethod {
        MyClass *myObject = [[MyClass alloc] init];
        [myObject setBackgroundColor:[UIColor blueColor]];
        [myarray addObject:myObject];
        [myObject release];
    }
    

    So you have an array with separate objects in it. You can iterate through it. You even can set the initial backgroundColor – when you tap the button – to a color based on the users choice. (in my example the color always would be blue)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering, when you create an object you can often set multiple attributes
I will create multiple sale versions of the same application with push notifications feature:
i want create multiple search where statement $where_search is a multiple condition from post
So I create multiple BackgroundWorkers using loop: foreach (var item in list) { worker
How can I create multiple dropdowns in my view with values coming from my
How do i create multiple checkbox for jquery to identify it? example i have
How do we create multiple thread during runtime in C programming language? If we
I want to create multiple versions of print-friendly pages from a single page. I
I'm trying to create multiple sheets by iteration in phpexcel: $i=0; while ($i <
Can we create multiple schemas for a particular user? I am currently logged in

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.