我已經動態地創建了一些帶有一些值的RadioButton列表。在語法上,我改變了按鈕狀態,並改變了圖像的選擇和未選中。但問題是我可以同時選擇所有的radioButtons。實際上我需要一次選擇一個。 當我點擊下一個RadioButton時,之前選擇的按鈕狀態應該改爲未選中狀態。自定義RadioButton一次爲iPhone選擇一個?
這是我的代碼,我嘗試改變圖像,但是...我的代碼有些問題。
RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
[RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
[RadioButton setCenter:CGPointMake(116.0,p1)];
[RadioButton setSelected:NO];
[RadioButton setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
[RadioButton addTarget:self action:@selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:RadioButton];
-(void)RadioButtonTapped:(id)sender
{
UIButton *RadioButton1 = (UIButton*)sender ;
[self radiobuttonAction:RadioButton1];
}
-(void)radiobuttonAction:(UIButton *)Button
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:@"radio_active.png"] forState:UIControlStateSelected]; //not working, button image is not changing
}
else
{
[Button setSelected:NO];
[Button setImage:[UIImage imageNamed:@"radio_inactive.png"] forState:UIControlStateNormal];
}
}
在哪裏可以更改以前選擇的按鈕的圖像。
在此先感謝
如何使按鈕上的插座,例如你有3個按鈕,使3個插座,所以它可以很容易地做你的問題。 – Bazinga
但我動態地創建了按鈕, – neerajPK
'RadioButtonTapped'--在此,你是否在每個按鈕中都使用了這個按鈕? – Bazinga