select - iOS: change background color using multiple buttons -
i trying change background color randomly effect overtime of buttons on screen tapped. control effect on/off uibutton. tapping changecolorbutton logs “off” never “on”. not sure do? everyone!!
edited code far!! in .h
@property(nonatomic,readwrite) bool shouldchangecolor; in .m - (ibaction)changecolorbutton:(uibutton*)sender { // self.shouldchangecolor = !sender.selected; sender.selected = !sender.selected; if(sender.selected) { nslog(@"switch on"); //make off // sender.selected=no; // self.shouldchangecolor=true; } else nslog(@"switch off"); //make on // sender.selected=yes; self.shouldchangecolor=true; } - (void)randomcolor{ int r = arc4random() % 255; int g = arc4random() % 255; int b = arc4random() % 255; uicolor *color = [uicolor colorwithred:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:1.0]; [self.view setbackgroundcolor:color]; }
because not changing selection of button
- (ibaction)changecolorbutton:(uibutton*)sender { //self.shouldchangecolor = sender.selected; sender.selected = !sender.selected; //if if(sender.selected) { self.shouldchangecolor=yes; nslog(@"switch on"); //make off //sender.selected=no; don't have [self randomcolor];//if want change color when switch on } else{ self.shouldchangecolor=no; nslog(@"switch off"); //make on //sender.selected=yes; don't have [self.view setbackgroundcolor:[uicolor blackcolor]];//check syntax } }
now anywhere call function change color [self randomcolor];
- (void)randomcolor{ if(self.shouldchangecolor){ int r = arc4random() % 255; int g = arc4random() % 255; int b = arc4random() % 255; uicolor *color = [uicolor colorwithred:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:1.0]; [self.view setbackgroundcolor:color]; } }
Comments
Post a Comment