Reputation: 17882
For my iOS app I have an array of text, and I want to generate inside of my UIScrollview a list of text on the right hand side and a list of buttons on the left hand side. Whenever a user presses a UIButton from the right hand side the text from the left hand side will be copied to the user's device's clipboard...
I figured out how to generate the list of text in UILabels using a for loop and an NSArray. I have no clue how to programmatically generate UIButtons though and I also don't know how to make each button copy the respective string from the array (i.e. button #3 copies string #3 (objectAtIndex #2) from the array)
Here is my code so far:
NSArray *copyText;
copyText = [NSArray arrayWithObjects: @"text to copy 1", "text to copy 2", "text to copy 3", "text to copy 4", nil];
int i = 0;
while (i < [copyText count]) {
UILabel *copyLabels = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (ScrollView1.frame.size.width*2/3), 25)];
copyLabels.center = CGPointMake(ScrollView1.frame.size.width*2/3, ((i*25)+15));
copyLabels.textAlignment = UITextAlignmentCenter;
copyLabels.textColor = [UIColor blackColor];
copyLabels.backgroundColor = [UIColor clearColor];
copyLabels.font = [UIFont fontWithName:@"System" size:(16.0)];
[ScrollView1 addSubview:copyLabels];
copyLabels.text = [NSString stringWithString:[copyLabels objectAtIndex:i]];
i ++;
}
Upvotes: 0
Views: 1352
Reputation: 17882
Found my answer here: Add a multiple buttons to a view programmatically, call the same method, determine which button it was
The UIButton has a "tag" field in which numeric values can be assigned so my loop can keep track of which button is which. Genius.
Upvotes: 1