I have a button that, when tapped, adds a label inside a view. Anytime it is tapped after that, it adds another label that starts where the last one ended.
I tried this
if (self.currentLabel == nil)
startingPoint = 0;
else
startingPoint = currentLabel.frame.size.width + 5;
// Most recently created label becomes currentLabel
self.currentLabel = [[FormulaLabel alloc] initWithFrame:CGRectMake(startingPoint, 10, 100, 50)];
To keep track of all the labels, I tried adding them to an array
[arrayOfObjects addObject:self.currentLabel]
but I noticed that the array count wasn’t changing.
Why doesn’t the above code work, and is there a better way to keep track of an indefinite amount of labels?
NSArrays are immutable. You need to use an NSMutableArray.
If you are already using a NSMutableArray, are you getting any warnings?
As an additional note, “arrayOfObjects” is a poor name. Something like “labels,” “usedLabels” or “labelHistory” would be far more descriptive.