我想在我的iOS应用中,用户可以点击UIButton
选择一些字段。
我希望用户在点击UIButton
时选择字段,按钮图像应更改并打开我的MMPickerView
,当用户再次点击时,UIButton
应取消选中并显示上一张图片在UIButton
中,此时不要打开MMPickerview
。
这是我的代码:
- (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;
}];
}
}
答案 0 :(得分:3)
UIImage *bimage=[UIImage imageNamed: @"play_select.jpg"];
[_button setImage:bimage forState:UIControlStateSelected];
[_button setImage:bimage forState:UIControlStateHighlighted];
声明按钮
答案 1 :(得分:2)
我猜你没有改变按钮的状态。试试这个......
- (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.
}];
}
}
并在您声明按钮的位置编写此代码。
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];
答案 2 :(得分:1)
要将图像设置为按钮,请使用setbackground图像而不是setimage
[<buttoninstane> setBackgroundImage:<#(UIImage *)#> forState:UIControlStateNormal];
[<buttoninstane> setBackgroundImage:<#(UIImage *)#> forState:UIControlStateSelected];
将上面的代码写在声明按钮的位置
在按钮操作中使用以下编辑的代码
- (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;
}];
}
}
答案 3 :(得分:1)
使用不同的按钮状态设置不同的图像
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
在您的代码中,if语句(_button.selected = !_button.selected)
将始终为true,因此else部分将永远不会运行。
答案 4 :(得分:0)
Swift 4
在声明按钮的地方添加
android:layout_weight