Search code examples
iosobjective-cuibuttonuipickerviewibaction

Change image of UIButton on select or unselect


I want in my iOS app user can select some field on clicking the UIButton.

I want when user selects the field on click of UIButton,image of button should change and open my MMPickerView and when user clicks again the UIButton should get unselected and display previous image in UIButton and do not open MMPickerview at this time.

This is my code:

 - (IBAction)showPickerViewButtonPressed:(id)sender {
      if( (_button.selected = !_button.selected))
      {
           UIImage *bimage=[UIImage imageNamed: @"play.jpg"];
           [_button setImage:bimage forState:UIControlStateNormal];
      } 
      else
      {
           [MMPickerView showPickerViewInView:self.view
                       withStrings:_stringsArray
                       withOptions:@{MMbackgroundColor: [UIColor blackColor],
                                     MMtextColor: [UIColor whiteColor],
                                     MMtoolbarColor: [UIColor blackColor],
                                     MMbuttonColor: [UIColor whiteColor],
                                     MMfont: [UIFont systemFontOfSize:18],
                                     MMvalueY: @3,
                                     MMselectedObject:_selectedString}
                        completion:^(NSString *selectedString) {

                            _label.text = selectedString;
                            _selectedString = selectedString;
             }];
         }
   }

Solution

  • I guess you are not changing button's state. Try this...

    - (IBAction)showPickerViewButtonPressed:(id)sender 
    {
       if ([_button isSelected])
       {
          _button.selected=NO;
       } 
       else
       {
          _button.selected=YES; // change button's state
               [MMPickerView showPickerViewInView:self.view
                       withStrings:_stringsArray
                       withOptions:@{MMbackgroundColor: [UIColor blackColor],
                                     MMtextColor: [UIColor whiteColor],
                                     MMtoolbarColor: [UIColor blackColor],
                                     MMbuttonColor: [UIColor whiteColor],
                                     MMfont: [UIFont systemFontOfSize:18],
                                     MMvalueY: @3,
                                     MMselectedObject:_selectedString}
                        completion:^(NSString *selectedString) {
    
                            _label.text = selectedString;
                            _selectedString = selectedString;
                         //    _button.selected=YES;  change button's state here If you want to change state after completion.
                        }];
       }
    }
    

    And write this code where you declared your button.

      UIImage *bSelectedimage=[UIImage imageNamed: @"your image name for selected state"]; 
      UIImage *bimage=[UIImage imageNamed: @"your image name for default state"]; 
     [_button setImage:bSelectedimage forState:UIControlStateSelected];
     [_button setImage:bimage forState:UIControlStateNormal];