我有一个 iPhone 应用程序,我想在每次点击按钮时更改按钮的背景图像,即如果它跟随它,则在点击它时需要取消关注,反之亦然。有人可以帮我吗?
问问题
372 次
4 回答
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.....
}
于 2012-05-10T12:31:40.680 回答
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
于 2012-05-10T12:33:14.640 回答
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];
}
于 2012-05-10T12:30:01.197 回答
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];
}
这将取决于您如何加载具有什么名称和图像数量的图像。因此,您可以自定义。
于 2012-05-10T12:32:18.603 回答