1

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]; 

}
}
}
Prasad G
  • 6,702
  • 7
  • 42
  • 65

3 Answers3

3

In your inner loop :

  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];
        if (x == 1 && y == 3) {
           [button setTitle:@"Set" forState:UIControlStateNormal];
        } else {
           [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];

    }
giorashc
  • 13,691
  • 3
  • 35
  • 71
  • do you know if I want to connect my button --> -(void)buttonPressed:(UIButton *)button -->to another view what should I have?thanks in advance! –  Jun 20 '12 at 07:38
  • A button can be connected to one view so you will need to separate the logic into a common method which will be used by both controllers – giorashc Jun 20 '12 at 09:00
1

You can just check if the value of the currentTag is equal to 8 and then set a different title:

if (currentTag == 8) {
    [button setTitle:@"Set" forState:UIControlStateNormal];
} else {
    [button setTitle:[NSString stringWithFormat:@"%d",currentTag]forState:UIControlStateNormal];
}
Kris
  • 5,714
  • 2
  • 27
  • 47
1
if (button.tag==yournumber) {
           [button setTitle:@"Set" forState:UIControlStateNormal];
        } else {
           [button setTitle:[NSString stringWithFormat:@"%d",currentTag]forState:UIControlStateNormal];
 }

Try like this. I think it will be helpful to you.

Prasad G
  • 6,702
  • 7
  • 42
  • 65
  • do you know if I want to connect my button --> -(void)buttonPressed:(UIButton *)button -->to another view what should I have? –  Jun 20 '12 at 07:38
  • Here [this link](http://stackoverflow.com/questions/10296573/connect-button-to-tableviewcontroller-in-xcode/10297146#10297146). – Prasad G Jun 20 '12 at 07:44
  • since I create this button programmatically not interface can i use that code? :(id)sender{ if (!createViewController) { createViewController = [[CreateViewController alloc] initWithNibName:@"CreateViewController" bundle:nil]; } –  Jun 20 '12 at 07:47
  • Just write a code in -(void)buttonPressed:(UIButton *)button. Replace -(void)buttonPressed:(UIButton *)button instead of -(IBAction)clickButton:(id)sender. – Prasad G Jun 20 '12 at 07:51
  • I add my method above in Edit 2 would you please look at it , it's not working –  Jun 20 '12 at 08:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12788/discussion-between-justin-and-prasad-g) –  Jun 20 '12 at 08:53
  • would you please write the information here I sent you the project but I don't have access to my email to check it , I appreciate it if you can write the solution here ,Thanks –  Jun 20 '12 at 09:53
  • did you have time to check that? –  Jun 20 '12 at 13:23
  • No. But i have sent sample example. – Prasad G Jun 20 '12 at 13:27