我希望能够在被释放的按钮上来回切换按钮的背景图像。 我已经以编程方式将按钮添加到我的视图控制器,并可以在按下时将其更改为图像。但我不确定一旦被释放我应该如何处理。
请帮忙!非常感谢。 以下是我的viewDidLoad中使用的代码。
UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
// position in the parent view and set the size of the button
shareButton.frame = CGRectMake(51, 216, 46, 36);
[shareButton setTitle:@"Share" forState:UIControlStateNormal];
// Add image to button for normal state
UIImage * shareImage1 = [UIImage imageNamed:@"noninverted_19.png"];
[shareButton setImage:shareImage1 forState:UIControlStateNormal];
// Add image to button for pressed state
UIImage * shareImage2 = [UIImage imageNamed:@"inverted_19.png"];
[shareButton setImage:shareImage2 forState:UIControlStateHighlighted];
// add targets and actions
[shareButton addTarget:self action:@selector(submitBookingButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
// add to a some parent view.
[eventScroller addSubview:shareButton];
答案 0 :(得分:0)
创建一个bool _firstImg
,然后尝试使用此代码(我从here获得了一些代码)
- (IBAction)submitBookingButtonPressed:(UIButton)sender {
NSString *imageName = @"image_1"
if (!_firstImg) {
imageName = @"image_2"
}
[sender setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
_firstImg = !_firstImg
}
将image_1
和image_2
替换为图片的名称。
请注意,我尚未对此进行测试,因此您可能需要修改部分语法。