我必须执行单个UIButton的togling,我已经从不同帧的循环生成了它

时间:2013-03-15 11:57:25

标签: ios loops uibutton toggle

在我的iOS应用程序中,我必须按按钮点击更改图像。  我必须执行单个UIButton的togling,我已经从不同帧的循环生成

如果我对单个按钮执行操作,它工作正常,如果我对多个按钮执行操作,则图像未按照按钮操作正确更改

这是我的按钮

btnFullScreen = [[UIButton alloc] init];
    [btnFullScreen setBackgroundImage:[UIImage imageNamed:@"open icon.png"] forState:UIControlStateNormal];
    [btnFullScreen setFrame:CGRectMake(self.view.frame.size.width-30,8,16,14)];
    [btnFullScreen setBackgroundImage:[UIImage imageNamed:@"close icon.png"] forState:UIControlStateSelected];
    [btnFullScreen addTarget:self action:@selector(showPopUp:) forControlEvents:UIControlEventTouchUpInside];
    btnFullScreen.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin;
    [self.view addSubview:btnFullScreen];

这是我的按钮操作代码

-(void)showPopUp:(id)sender{
if ([sender isSelected]) {
    [sender setImage:[UIImage imageNamed:@"open icon.png"] forState:UIControlStateNormal];
    [sender setSelected:NO];
}else {
    [sender setImage:[UIImage imageNamed:@"close icon.png"] forState:UIControlStateSelected];
    [sender setSelected:YES];
}
[self.parent.parent addPopOversExcept:self.parent];

}

由于

1 个答案:

答案 0 :(得分:0)

我之前在我的一个应用程序中做过这个。
我在每个tableview单元格中创建了按钮。

所以,请将此代码作为Reference

1.特定方法。

- (void)methodName:(UIButton *)sender
 {
int i = [sender.titleLabel.text intValue];
NSNumber *numb;
if(i == 0)
{
    numb = [NSNumber numberWithBool:NO];
    sender.titleLabel.text = @"1";
    [sender setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateNormal];
    [sender setImage:[UIImage imageNamed:@"up.png"] forState:UIControlStateHighlighted];
}
else
{
    numb = [NSNumber numberWithBool:YES];
    sender.titleLabel.text = @"0";
    [sender setImage:[UIImage imageNamed:@"up.png"] forState:UIControlStateNormal];
    [sender setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateHighlighted];
}
}

2.在UITabelview viewForHeaderInSection中以编程方式设置UIButton。

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(240, 20, 30, 30)];

[button addTarget:self
           action:@selector(methodName:)
 forControlEvents:UIControlEventTouchDown];
button.tag = section;
if([[sectionsArray objectAtIndex:section] boolValue])
{
    [button setImage:[UIImage imageNamed:@"up.png"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateHighlighted];
    button.titleLabel.text = @"0";
}
else
{
    [button setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"up.png"] forState:UIControlStateHighlighted];
    button.titleLabel.text = @"1";
}