uibutton - iOS how do I reset button images? -
i have few buttons when pressed change plain button button picture of tick. problem have can't ticked button untucked button when separate button pressed. possible @ all?
here buttons:
- (ibaction)pickcat:(uibutton *)sender { [self deselectallbuttons]; ((uibutton *)sender).selected = true; uiimage *btnimage1 = [uiimage imagenamed:@"white - on.png"]; [sender setimage:btnimage1 forstate:uicontrolstatenormal]; self.catlabel.text = @"white (hi-po)"; } - (ibaction)pickcatb:(uibutton *)sender { [self deselectallbuttons]; ((uibutton *)sender).selected = true; uiimage *btnimage1 = [uiimage imagenamed:@"red - on.png"]; [sender setimage:btnimage1 forstate:uicontrolstatenormal]; self.catlabel.text = @"red (significant)"; } - (ibaction)pickcatc:(id)sender { uiimage *btnimage1 = [uiimage imagenamed:@"yellow - on.png"]; [sender setimage:btnimage1 forstate:uicontrolstatenormal]; self.catlabel.text = @"yellow (serious)"; } - (ibaction)pickcatd:(id)sender { uiimage *btnimage1 = [uiimage imagenamed:@"green - on.png"]; [sender setimage:btnimage1 forstate:uicontrolstatenormal]; self.catlabel.text = @"green (important)"; } - (void)deselectallbuttons { // assuming have reference buttons self->btn1.selected = false; self->btn2.selected = false; }
i set different image each state: normal , selected. looks you're using interface builder, , it's easy set buttons different images normal , selected state.
then in code can this:
- (ibaction)pickimage:(id)sender { [self deselectallbuttons]; sender.selected = true; self.loclabel.text = @"red"; } - (ibaction)pickimageb:(id)sender { [self deselectallbuttons]; sender.selected = true; self.loclabel.text = @"red"; } - (void)deselectallbuttons { // assuming have reference buttons self.btn1.selected = false; self.btn2.selected = false; ... }
if want able reset buttons before changing 1 checked image, have have reference else want affect. if want fancy, outlet collections can loop through array of buttons ib file , set selected state false.
update:
here's how code should look:
- (ibaction)pickcat:(uibutton *)sender { [self deselectallbuttons]; // no need cast since changed method signature. // system expects sender button // ((uibutton *)sender).selected = true; sender.selected = true; // no need set image explicitly. if did things correctly // in ib, you've told button image use when // selected, , image use when not (default). when set // line above (sender.selected = true), tell button // go use image added button's selected state in ib // uiimage *btnimage1 = [uiimage imagenamed:@"white - on.png"]; // [sender setimage:btnimage1 forstate:uicontrolstatenormal]; self.catlabel.text = @"white (hi-po)"; } - (ibaction)pickcatb:(uibutton *)sender { [self deselectallbuttons]; sender.selected = true; self.catlabel.text = @"red (significant)"; } - (ibaction)pickcatc:(id)sender { [self deselectallbuttons]; sender.selected = true; self.catlabel.text = @"yellow (serious)"; } - (ibaction)pickcatd:(id)sender { [self deselectallbuttons]; sender.selected = true; self.catlabel.text = @"green (important)"; } - (void)deselectallbuttons { // 2 things: // 1) i'm not sure self->btn1. why not self.btn1? // 2) looks have 4 buttons (cata, catb, catc, catd). if so, need set selected state of them false self->btn1.selected = false; self->btn2.selected = false; self->btn3.selected = false; self->btn4.selected = false; }
Comments
Post a Comment