Hi I want to have 2 different title for my buttons, I have 7 buttons with 1 to 7 number title and also 8 buttons that I want to have "Set" as a title,
Would you please help me to know how specify title in a loop?
Thanks in advance!
here is the picture that I have, I want to have 'Set' title instead of 8 in my last button.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
int rows = 4, columns = 2;
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 200*columns, 105*rows)];
int currentTag = 0;
for (int y = 0; y < rows; y++) {
// currentTag++;
for (int x = 0; x < columns; x++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor=[UIColor colorWithRed: 201.0/255.0 green: 201.0/255.0 blue:201.0/255.0 alpha: 1.0];
button.tag = currentTag; // ADDED
currentTag++;
[button.layer setBorderColor: [[UIColor blackColor] CGColor]];
[button.layer setBorderWidth: 1.0];
[button setTitle:[NSString stringWithFormat:@"%d",currentTag] forState:UIControlStateNormal];
button.frame = CGRectMake(200*x, 105*y, 200, 105);
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
}
}
// Center the view which contains your buttons
CGPoint centerPoint = buttonView.center;
centerPoint.x = self.view.center.x;
buttonView.center = centerPoint;
[self.view addSubview:buttonView];
}
-(void)buttonPressed:(UIButton *)button
{
NSLog(@"button %u -- frame: %@", button.tag, NSStringFromCGRect(button.frame));
}
Edit 2 for connecting another view to the button
FirstViewController.h
@interface FirstViewController : UIViewController{
SecondViewController *createViewController;
}
-(void)buttonPressed:(UIButton *)button;
@end
my button code
-(void)buttonPressed:(UIButton *)button
{
NSLog(@"button %u -- frame: %@", button.tag, NSStringFromCGRect(button.frame));
{ if (!createViewController) { createViewController = [[TDDSecondViewController alloc] initWithNibName:@"TDDSecondViewController" bundle:nil];
}
}
}