How do I add a new object an unlimited number of times at runtime?
Say I have a label on the screen, and a button. When the user presses the button, it makes a new label underneath the one already there, and it can do this an unlimited number 开发者_StackOverflowof times.
I don't need the code for making a new position. I'm just wondering how I would add a new label? Can I do the same with a bunch of variables related to it too, like say create a few NSStrings?
What you have to do is to create an UILabel view and add it to the current view as a subview programmatically.
NSMutableArray *labelArray; /* assumes it has your other labels */
UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(/* your frame to locate the label */)];
newLabel.text = @"whatever";
[self.view insertSubview:newLabel below:[labelArray lastObject]];
[labelArray addObject:newLabel];
[newLabel release];
UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectZero];
newLabel.text = @"Some text";
[self.view insertSubview:newLabel belowSubview:originalLabel];
精彩评论