2015-10-26 83 views
-1

我有兩個自定義的圓形矩形按鈕,並在此做了圖像show.like類似點擊事件當我們選擇性別只有一個選項選擇其他自動hide.but我給我當我觸摸第一個按鈕比它選擇,但當我觸摸第二個按鈕沒有操作選擇。 我的主要moto是,我想選擇性別,但在iOS沒有單選按鈕使用,所以我設計自定義和點擊事件設置圖像。調用api是一個字符串類型。在iOS設置只有一個選項

 -(void)viewdidload 
     { 
      button = [UIButton buttonWithType:UIButtonTypeCustom]; 
      [email protected]"MALE";//calling Api is a string type// 
      [button addTarget:self action:@selector(roundButtonDidTap:) forControlEvents:UIControlEventTouchUpInside]; 
     } 

     -(void)roundButtonDidTap:(UIButton*)tappedButton 
     { 
      if([callApi isEqualToString:@"FEMALE"]) { 
       UIImage *btnImage = [UIImage imageNamed:@"bullet.png"]; 
       [button setImage:btnImage forState:UIControlStateNormal];        
      } 

      if ([callApi isEqualToString:@"MALE"]) {       
       UIImage *btnImage = [UIImage imageNamed:@"bullet.png"]; 
       [button2 setImage:btnImage forState:UIControlStateSelected]; 
       button.imageView.hidden=YES;    
      } 
     } 

回答

0

試試這個代碼:

 -(IBAction)selectMale:(id) sender{ // MALE BUTTON 
     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]; 
      gender = @"Male"; 
      [btnFemale setImage:[UIImage imageNamed:@"radio.png"] forState:UIControlStateSelected]; 
      [btnFemale setSelected:NO]; 

     } 
     else 
     { 
      [Button setSelected:NO]; 
      [Button setImage:[UIImage imageNamed:@"radio.png"] forState:UIControlStateNormal]; 
     } 
     } 

     -(IBAction)selectFemale:(id) sender{ 
     UIButton *RadioButton1 = (UIButton*)sender; 
     [self radiobuttonActionFemale:RadioButton1]; 
     } 
     -(void)radiobuttonActionFemale:(UIButton *)Button 
     { 
     if(![Button isSelected]) 
     { 
      [Button setSelected:YES]; 
      [Button setImage:[UIImage imageNamed:@"radio_active.png"] forState:UIControlStateSelected]; 
      gender = @"Female"; 
      [btnMale setImage:[UIImage imageNamed:@"radio.png"] forState:UIControlStateSelected]; 
      [btnMale setSelected:NO]; 
     } 
     else 
     { 
相關問題