0

I have created 3 custom buttons using for loop. But when I select a button the text color is not changing.How do I do it? What else I have to add?

Here is my Code

buttonText = [[NSArray alloc]initWithObjects: @"Slambook",@"Initiated",@"Collaborated",nil];    
NSInteger numControllers = [viewControllerArray count];

for (int i = 0; i<numControllers; i++) {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
    [button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateSelected];
    [button setTitle:[buttonText objectAtIndex:i] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [navigationView addSubview:button];
}
Zeen
  • 35
  • 2
  • 8

2 Answers2

0
    NSInteger numControllers = [viewControllerArray count];

    for (int i = 0; i<numControllers; i++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
        [button setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1]];
        [button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [navigationView addSubview:button];
    }
- (IBAction)tapButtonAction:(id)sender
{

    UIButton *btn = (UIButton*)sender;
    if ([btn isSelected]) {
        [btn setSelected:NO];
        [btn setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];

    }
    else
    {
        [btn setSelected:YES];
        [btn setBackgroundColor:[UIColor colorWithRed:204.0/255.0 green:24.0/255.0 blue:204.0/255.0 alpha:1] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    }
}

change your for loop with this code

int x = 0;
    for (int i = 0; i < [viewControllerArray count]; i++){
        //int y=2;
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x+2, 0, 40, 36)];

        id myArrayElement=[viewControllerArray objectAtIndex:i];

        [button setTitle:[NSString stringWithFormat:@"%@",myArrayElement] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackcolor]];

        button.titleLabel.font = [UIFont systemFontOfSize:15];


        x += button.frame.size.width;

        [button setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
  • It changes the color when clicked. But when I click on other buttons it remains the same color I want change the unselected to grey and other button is selected – Zeen Nov 30 '17 at 10:19
  • some changes in tapButtonAction so refer it again –  Nov 30 '17 at 11:29
0

I have tried your code for single button with dummy text , black text colour for normal state and button colour is highlighting on tap:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
     button.frame = CGRectMake(20, 40,50 , 35);
//        button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
    [button setTitle:@"Dummy" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateHighlighted];

        [button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];

Below line does not change button text colour for long time, it change text colour of button for fraction of seconds after tap

[button setTitleColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateHighlighted];

Try this:

 NSInteger numControllers = [viewControllerArray count];

    for (int i = 0; i<numControllers; i++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER,(self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT);
   button.tag=i+200;
        [button setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1]];
        [button addTarget:self action:@selector(tapButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [navigationView addSubview:button];
    }
- (IBAction)tapButtonAction:(id)sender
{

    UIButton *btn = (UIButton*)sender;

        [btn setBackgroundColor:[UIColor colorWithRed:137.0/255.0 green:110.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];

   for (int i = 0; i<numControllers; i++) {
       UIButton *restBtn=[navigationView viewWithTag:i+200];
    if(restbtn!= btn)
    {
         [restBtn setBackgroundColor:[UIColor grayColor] forState:   UIControlStateNormal];
        [restBtn setTitleColor:[UIColor blackcolor] forState:UIControlStateNormal];
}

    }
}
Mangesh Murhe
  • 361
  • 1
  • 8
  • yeah . I have the same issue but want that color to stay until I select other button – Zeen Nov 30 '17 at 10:21
  • I tried it there as @JatinRB Korat answer. but once it is set it is not setting back to unselected color when other button is tapped – Zeen Nov 30 '17 at 11:00
  • I have edited answer here only.Try this.may you get some syntax error as i have edited code here only.But i hope it will solve your problem – Mangesh Murhe Nov 30 '17 at 11:16
  • It crashes saying `Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setTitleColor:forState:]: unrecognized selector sent to instance 0x7faeb741c400'` . I tried changing `[navigationView viewWithTag:i*200]` to `UIButton *restBtn= (UIButton *)[navigationView viewWithTag:i];` still it is crashing – Zeen Nov 30 '17 at 12:07
  • You have passed only 'i', because of that tag is not matching.Use i*200 then only tag will match – Mangesh Murhe Nov 30 '17 at 12:18
  • While setting also I have set as `button.tag=i` – Zeen Nov 30 '17 at 12:30
  • When you get button by using tag, check type of btn object. It should be uibutton. By seeing crash log i think button object is not type casted to Uibutton. It is remaining as UIView. – Mangesh Murhe Nov 30 '17 at 12:34
  • since I have 3 controllers it gives me 3 objects one of UIView and other two of UIButton – Zeen Nov 30 '17 at 12:37
  • Can you post the comsole log?? – Mangesh Murhe Nov 30 '17 at 12:42
  • `restBtn > 2017-11-30 18:16:39.810246+0530 Wishfie[8200:284625] restBtn > 2017-11-30 18:16:39.810726+0530 Wishfie[8200:284625] restBtn > ` . this is displayed in the log – Zeen Nov 30 '17 at 12:47
  • This is because your other view and some buttons have similar tag. That's why i used i*200 to make unique tag. Change it to i*1000 at every place – Mangesh Murhe Nov 30 '17 at 12:55
  • HI Zeenath.Sry for mistake.Use i+200. It is working fine.Problem was with i*200 when i is 0.So tag for buttons set to 0, which is by default tag for UIView.Use i+200 .It is working. – Mangesh Murhe Nov 30 '17 at 13:15
  • I have also edited my answer.I hope you will get your solution this time. – Mangesh Murhe Nov 30 '17 at 13:18