mercredi 2 septembre 2015

Changing a button background colour using RGB

Im sure that it will be a simple and obvious fix, but I cannot see why this occurs.

Basically I want to code a button to change its background colour, depending on what the colour is at the present. The following code works but it uses the UI Colours in xcode.

-(IBAction)Button1Colour:(id)sender{

if (_Button1.backgroundColor == nil) {
    _Button1.backgroundColor = [UIColor orangeColor];}

else if (_Button1.backgroundColor == [UIColor orangeColor]) {
    _Button1.backgroundColor = [UIColor greenColor];}

else (_Button1.backgroundColor = nil);}

Now if I use RGB colours as follows:

-(IBAction)Button1Colour:(id)sender{

if (_Button1.backgroundColor == nil) {
    _Button1.backgroundColor = [UIColor colorWithRed:(216/255.f) green:(146/255.f) blue:(48/255.f) alpha:0.3];}

else if (_Button1.backgroundColor == [UIColor colorWithRed:(216/255.f) green:(146/255.f) blue:(48/255.f) alpha:0.3]) {
    _Button1.backgroundColor = [UIColor colorWithRed:(33/255.f) green:(141/255.f) blue:(8/255.f) alpha:0.3];}

else (_Button1.backgroundColor = nil);}

The button will only cycle from nil to amber to nil etc missing out the else if statement.

Any advice or help would be much appreciated

Aucun commentaire:

Enregistrer un commentaire