Reputation: 1595
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;
}];
}
}
Upvotes: 2
Views: 2954
Reputation: 536
Swift 4
add this where button is declared
let bimage = UIImage(named: "play_select.jpg")
button.setImage(bimage, for: .selected)
button.setImage(bimage, for: .highlighted)
Upvotes: 0
Reputation: 811
For setting image to the button use the setbackground image instead of setimage
[<buttoninstane> setBackgroundImage:<#(UIImage *)#> forState:UIControlStateNormal];
[<buttoninstane> setBackgroundImage:<#(UIImage *)#> forState:UIControlStateSelected];
Write the above code where the button is declared
And in the button action use the below edited code
- (IBAction)showPickerViewButtonPressed:(id)sender {
if(_button.selected ){
[_button setSelected:NO];
}
else{
[_button setSelected:YES];
[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;
}];
}
}
Upvotes: 1
Reputation: 5754
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];
Upvotes: 2
Reputation: 11
To set different image in different button status use
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
In your code the if statement (_button.selected = !_button.selected)
will always be true, so the else part will never run.
Upvotes: 1
Reputation: 213
UIImage *bimage=[UIImage imageNamed: @"play_select.jpg"];
[_button setImage:bimage forState:UIControlStateSelected];
[_button setImage:bimage forState:UIControlStateHighlighted];
where button is declared
Upvotes: 3