Creating a Mutable array that can be added to later clicks of the same button?

General noob questions:

(1) How can I create an NSMutable array in a NSMutable action to which I can add more entries during subsequent clicks of the same button? I always start with a new array each time I click (the array prints with only 1 entry, which is the most recent button tag in the NSLog statement).

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

I want to track how many (and which) buttons were pressed , with the ability to delete these entries, if they are pressed 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 read that I can use the plist dictionary, but I really don’t understand how to do this in the code, because I cannot print elements in the dictionary manually (since I don’t know which buttons the user clicks). Would it be easier if I somehow downloaded and replaced the dictionary in the plist file? And how would I do that?

(3) I also don’t know how to manage memory, since I need to constantly update the array. autorelease ?

Thanks for any help you can provide!

+4
source share
1 answer

Well, firstly, you create a locally restricted array that is reinitialized with every call to buttonClicked:. The variable must be part of the init loop of the class.

You will also be better off using NSMutableDictionary instead of NSMutableArray. With the dictionary, we do not need to specify the capacity, and we can use button tags as dictionary keys.

Here is what you need to do, these three steps always go together: property / synthesize / release . Good 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]; } 

Then we create the storage object when the class instance is initialized. Add this to your init class 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); } 
+3
source

All Articles