-1

I have code that counts upwards when buttons on my app are pressed, but I'd also like to change the button's background color once it has been tapped.

Can I simply add a backgroundColor to the below code?

- (IBAction)buttonPressed {
count++;
totalLabel.text = [NSString stringWithFormat:@"Hours\n%li",(long)count];
}
@end
NobodyNada
  • 7,529
  • 6
  • 44
  • 51
McD
  • 173
  • 2
  • 8
  • For a similar question, see: http://stackoverflow.com/questions/35660318/how-to-change-bgcolour-of-button-when-tapped-in-swift/35660770#35660770 – PyPiePi Feb 26 '16 at 20:09

1 Answers1

1

Sure. The easiest thing to do is change your IBAction so it receives the button that's been tapped.

-(IBAction)buttonPressed:(UIButton*)button {
   button.backgroundColor = [UIColor redColor];
}

(You'll need to reconnect the outlet from the nib as well so it knows that the new selector is buttonPressed:, not just buttonPressed.

jsd
  • 7,673
  • 5
  • 27
  • 47