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];
}
I have tried your code for single button with dummy text , black text colour for normal state and button colour is highlighting on tap:
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: