2016-12-03 64 views
2

我有2個單選按鈕男性,女性。我做了所有事情......當我嘗試選擇男性,女性radiobuton。它根本沒有選擇。這裏我的代碼:單選按鈕不改變,通過選擇

@IBOutlet var btn_male: UIButton! // create outlet for radio button 
@IBOutlet var btn_female: UIButton! 

@IBAction func btn_male_female_click(sender: UIButton) { 
     switch sender.tag { 
     case 10: 
      //male select 
      if (sender.selected == true) { 
       sender.selected = false; 
       btn_male.selected = true; 
       btn_female.selected = false; 
       gender = "1" 
       print("male selected") 

      }else{ 
       sender.selected = true; 
       btn_male.selected = false; 
       btn_female.selected = true; 
       gender = "2" 
       print("female selected") 
      } 
      break; 
     case 20: 
      //female select 
      if (sender.selected == true) { 
       sender.selected = false; 
       btn_female.selected = true; 

       btn_male.selected = false; 
       gender = "2" 
       print("female selected") 
      }else{ 
       sender.selected = true; 
       btn_female.selected = false; 
       btn_male.selected = true; 
       gender = "1" 
       print("male selected") 
      } 

      break; 
     default: 
      break; 
     } 
    } 

我不知道爲什麼不選擇。要更改圖像,我將狀態配置更改爲選中狀態,並保存選定的廣播圖像。

但我的問題是,它不是在所有selecting.Please幫我out.Thanks

UPDATE:

後,我更新此解決方案。我只是從我的數據庫檢查,我正在擺脫他們已經保存的特定用戶的性別。但我有一個問題。

例如,如果我選擇男性,我保存它。當我回到我的個人資料屏幕我的男性按鈕顯示爲選定。這是來自db沒有問題。但是,當我想更改單選按鈕從男性到女性,當我按下女性單選按鈕。我的男性單選按鈕仍然是選定的,它不會改變以取消選擇。如果我這樣做了兩次,那麼它的變化。

@IBAction func btn_male_female_click(sender: UIButton) { 
     self.selectedButton.selected = false 
     self.selectedButton = sender 
     self.selectedButton.selected = true 
     gender = "male" 
     if sender == btn_female { 
      gender = "female" 
     } 
     else if sender == btn_male { 

      gender = "male" 
     } 
} 
override func viewDidAppear(animated: Bool) { 
     super.viewDidAppear(true) 

if profileData.Gender .isEmpty { 





     }else{ 
      if profileData.Gender == "male" 
      { 
       self.btn_male.selected = true 
       self.btn_female.selected = false 
      } 
      else if profileData.Gender == "female" 
      { 
       self.btn_male.selected = false 
       self.btn_female.selected = true 
      } 

     } 
} 
+0

更改爲選擇的按鈕圖像,使用'setImage分配:...'方法,你需要,因此,兩個圖像。 –

+0

其代碼已經正常工作 –

+0

@mack在你的情況下,你使用這個庫,所以你必須根據該庫編寫代碼。 –

回答

2

您可以通過簡單地創建聲明的UIButton多了一個實例,它擁有像這樣的這種兩個按鈕之間的選擇按鈕解決這個問題。

@IBOutlet var btn_male: UIButton! // create outlet for radio button 
@IBOutlet var btn_female: UIButton! 
var selectedButton = UIButton() 

現在使用像這樣的按鈕動作。

@IBAction func btn_male_female_click(sender: UIButton) { 
    self.selectedButton.selected = false 
    self.selectedButton = sender 
    self.selectedButton.selected = true 
    gender = "1" 
    if sender == btn_female { 
     gender = "2" 
    } 
} 

編輯:您可以管理選定的屬性這樣的方式。

if !(profileData.Gender.isEmpty) { 

    if profileData.Gender == "male" 
    { 
     self.selectedButton = self.btn_male 
    } 
    else if profileData.Gender == "female" 
    { 
     self.selectedButton = self.btn_female    
    } 
    self.selectedButton.selected = true 
} 
+0

它在這一行中引發錯誤'gender =(sender == btn_male)? 「1」; :「2」'請求我添加更多的逗號 – mack

1

在你的代碼的問題是

case 10: 
     //male select 
     if (sender.selected == true) { 
      sender.selected = false; // btn_male selected "false" 
      btn_male.selected = true; // btn_male selected "true" that's the problem 
      btn_female.selected = false; 
      gender = "1" 
      print("male selected") 

     } 

我希望這個代碼將幫助您

@IBAction func btn_male_female_click(sender: UIButton) { 
    switch sender.tag { 
    case 10: 
     //male select 
     if (sender.selected == true) { 
      sender.selected = false; 
      btn_female.selected = false; 
      gender = "1" 
      print("male selected") 

     }else{ 
      sender.selected = true; 
      btn_female.selected = true; 
      gender = "2" 
      print("female selected") 
     } 
     break; 
    case 20: 
     //female select 
     if (sender.selected == true) { 
      sender.selected = false; 
      btn_male.selected = false; 
      gender = "2" 
      print("female selected") 
     }else{ 
      sender.selected = true; 
      btn_male.selected = true; 
      gender = "1" 
      print("male selected") 
     } 

     break; 
    default: 
     break; 
    } 
}