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 5990983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:19:19+00:00 2026-05-22T23:19:19+00:00

General noob questions: (1) How can I create an NSMutable array in a buttonClicked

  • 0

General noob questions:

(1) How can I create an NSMutable array in a buttonClicked action that I can add more entries to during subsequent clicks of the same button? I always seem to start over with a new array at every click (the array prints with only 1 entry which is the most recent button’s tag in an NSLog statement).

I have about 100 buttons (one for each character in my string called “list”) generated by a for-loop earlier in my code, and each has been assigned a tag. They are in a scrollview within the view of my ViewController.

I wish to keep track of how many (and which ones) of the buttons have been clicked with the option of removing those entries if they are clicked a second time.

This is what I have so far:

-(void) buttonClicked:(UIButton *)sender
      NSMutableArray * theseButtonsHaveBeenClicked = [[NSMutableArray alloc] initWithCapacity: list.length];
      NSNumber *sendNum = [NSNumber numberWithInt:sender.tag];
      [theseButtonsHaveBeenClicked addObject:sendNum at index:sender.tag];
      NSLog(@"%@",theseButtonsHaveBeenClicked);
}

(2) I have read that I may be able to use a plist dictionary but I don’t really understand how I would accomplish that in code since I cant type out the items in the dictionary manually (since I don’t know which buttons the user will click). Would this be easier if I somehow loaded and replaced the dictionary in a plist file? And how would I do that?

(3) I also have no idea how I should memory manage this since I need to keep updating the array. autorelease?

Thanks for any help you can provide!

  • 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-22T23:19:20+00:00Added an answer on May 22, 2026 at 11:19 pm

    Okay, firstly you are creating a locally scoped array that is being re-initialised on every call to buttonClicked:. The variable should be part of the class init cycle.

    You will also be better off with an NSMutableDictionary instead of an NSMutableArray. With a dictionary we don’t have to specify capacity and we can use the button’s tags as dictionary keys.

    Here’s what you need to do, these three steps always go together: property/synthesize/release. A good one to remember.

      //Add property declaration to .h file
      @property (nonatomic, retain) NSMutableDictionary * theseButtonsHaveBeenClicked;
    
      //Add the synthesize directive to the top of .m file
      @synthesize theseButtonsHaveBeenClicked;
    
      // Add release call to the dealloc method at the bottom of .m file
      - (void) dealloc {
        self.theseButtonsHaveBeenClicked = nil; // syntactically equiv to [theseButtonsHaveBeenClicked release] but also nulls the pointer
        [super dealloc];
      }
    

    Next we create a storage object when the class instance is initialised. Add this to your class’s init or viewDidLoad method.

     self.theseButtonsHaveBeenClicked = [[NSMutableDictionary alloc] dictionary]; // convenience method for creating a dictionary
    

    And your updated buttonClicked: method should look more like this.

        -(void) buttonClicked:(UIButton *)sender {
    
             NSNumber *senderTagAsNum = [NSNumber numberWithInt:sender.tag];
             NSString *senderTagAsString = [[NSString alloc] initWithFormat:@"%@",senderTagAsNum];
    
             // this block adds to dict on first click, removes if already in dict
             if(![self.theseButtonsHaveBeenClicked objectForKey:senderTagAsString]) {
               [self.theseButtonsHaveBeenClicked setValue:senderTagAsNum forKey:senderTagAsString];
             } else {
               [self.theseButtonsHaveBeenClicked removeObjectForKey:senderTagAsString];                }
             [senderTagAsString release];
             NSLog(@"%@", self.theseButtonsHaveBeenClicked);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For general protocol message exchange, which can tolerate some packet loss. How much more
In general I think I can convey most programming related concepts quite well. Yet,
I'm trying to set up a model (client) that contains some general attributes about
hey, i'm a noob to c++ (and coding in general) i'm looking for an
The general rule, only objects allocated in the free store can cause memory leaks.
For example I have general code for catching json, that says - its error
General: I have a standards compliant C++ API that uses Boost libraries that I
I am a complete Android and C in general noob, I have done a
General question, when do people access their DAL in a prism application? That is
In general, what I've observed using nm on Linux is that T symbol names

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.