我有一个iPhone应用程序,我想在每次点击时更改按钮的背景图像,即如果它被跟随它需要在点击它时取消关注和反面。可以有人帮助我吗?
答案 0 :(得分:1)
如果您想使用按钮作为切换按钮而不是使用标志(BOOL)变量并根据您的要求进行设置。但是,如果你设置的按钮图像不止一次,请使用它,如下图所示。
-(IBAction)buttonPress:(id)sender
{
if([sender tag] == 1)
{
[yourbutton setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
btnSegment1.tag = 2;// Here i reset tag. you can set tag according your requirements so you can use button next for next click as you want
}
else if([sender tag] == 2)
{
[yourbutton setBackgroundImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateNormal];
yourbutton.tag = 3;// Here i reset tag. you can set tag according your requirements so you can use button next for next click as you want
}
// and so on.....
}
答案 1 :(得分:1)
@interface XXX: UIViewController // whatever
{
BOOL btnImageState;
}
// etc.
@implementation XXX
- (id)init
{
if ((self = [super init]))
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setImage:[UIImage imageNamed:@"follow.png"] forState:UIControlStateNormal];
[btn setFrame:CGRectMake(x, y, w, h)];
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
return self;
}
- (void)click:(UIButton *)btn
{
if (btnImageState)
[btn setImage:[UIImage imageNamed:@"follow.png"] forState:UIControlStateNormal];
else
[btn setImage:[UIImage imageNamed:@"unfollow.png"] forState:UIControlStateNormal];
btnImageState = !btnImageState;
}
@end
答案 2 :(得分:0)
我在这里以编程方式完成。希望这会对你有所帮助。
-(IBAction) pause:(id)sender
{
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self
action:@selector(aMethod)
forControlEvents:UIControlEventTouchDown];
UIImage *buttonImage = [UIImage imageNamed:@"buttonImage.png"];
[button1 setBackgroundImage:buttonImage forState:UIControlStateNormal];
button1.frame = CGRectMake(208, 380.0, 60.0, 60.0);
[self.view addSubview:button1];
}
- (IBAction) aMethod
{
button1.hidden=YES;
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self
action:@selector(pause:)
forControlEvents:UIControlEventTouchDown];
UIImage *buttonImage = [UIImage imageNamed:@"Button2Image.png"];
[button1 setBackgroundImage:buttonImage forState:UIControlStateNormal];
//[button1 setTitle:nil forState:UIControlStateNormal];
button1.frame = CGRectMake(208, 380.0, 60.0, 60.0);
[self.view addSubview:button1];
}
答案 3 :(得分:0)
static int tapCount = 0;
- (IBAction) buttonTapped :(id)sender {
tapCount++;
NSString *imageName = [NSString stringWithFormat:@"image%d.png",tapCount];
[yourButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}
取决于您如何使用什么名称和图像数量加载图像。因此,您可以自定义。