2012-06-07 32 views
1

時... 的了ControlState-被默認設置爲正常使用圖像設置更改UIButton的形象不同的按鈕被點擊

當用戶點擊一個按鈕,它改變形象該按鈕,因爲我設置該按鈕的目標以符合所需的方法。我需要改變所有其它單選按鈕的圖像時, - (空)RADIOBUTTONAClicked:被稱爲

-(void)RadioButtonACLicked:{ 
//code to change radioButton A, the code i have works 
// Code to change all other buttons does not work. 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ .... //配置單元...

​​

ButtonB創作

radioButtonB = [UIButton buttonWithType:UIButtonTypeCustom]; 

[radioButtonB addTarget:self action:@selector(radioButtonBClicked:)forControlEvents:UIControlEventTouchUpInside]; 
[radioButtonB setImage:img2 forState:UIControlStateNormal]; 

所以被點擊單選按鈕時,我所需要的所有其它按鈕圖像設置爲相反。

回答

1

花了一段時間得到它的工作

(void)radioButtonClicked:(UIButton *)sender 
{ 
... 

// Sets the UIImage for the custom RadioButtons. 
//img refers to radioOn and img2 refers to radioOff. 
// uses an if esle conditional block inorder to correctly change the Images if teh conditions are met. 

for(int i=0;i<[self.radioButtons count];i++){ 
    if ([[self.radioButtons objectAtIndex:i]tag] == sender.tag) { 
    [[self.radioButtons objectAtIndex:i] setImage:img2 forState:UIControlStateNormal]; 

} 

[sender setImage:img forState:UIControlStateNormal]; 

} 
0

執行以下

//When creating the button, set their tags 
radioButtonA = [UIButton buttonWithType:UIButtonTypeCustom]; 
//Add this line 
radioButtonA.tag = 111; 

radioButtonB = [UIButton buttonWithType:UIButtonTypeCustom]; 
//Add this line 
radioButtonB.tag = 112; 

現在

-(void)RadioButtonACLicked:{ 
    //code to change radioButton A, the code i have works 
    //Code to change all other buttons does not work. 
    radioButtonA = [self.view viewWithTag:111]; 
    radioButtonB = [self.view viewWithTag:112]; 
    //Change the images for radioButton A and B as you wish 
} 
+0

不兼容的指針賦值 – KING