2016-11-29 71 views
-2

我有一些文本字段,當用戶輸入任何值時,我將通過按保存按鈕將所有內容發送到服務器。這是代碼:如何將單選按鈕選擇發送到服務器

@IBAction func addUserBtnPressed(sender: AnyObject) { 
    if validateProfileData() { 
     profileData.FirstName = self.data["FirstName"]! 
     profileData.LastName = self.data["LastName"]! 
     profileData.DateOfBirth = self.data["DateOfBirth"]! 
     profileData.Email = self.data["Email"]! 
     profileData.Mobile = self.data["Mobile"]! 

     profileData.Country = self.data["Country"]! 
     profileData.State = self.data["State"]! 
     profileData.City = self.data["City"]! 

     let toViewController = NextVC 
    } 
} 

但我有兩個單選按鈕,選擇男性或女性的性別。我正在使用此庫來選擇性別Radiobutton。但我不知道如何通過按保存按鈕發送到服務器。請幫我解決..!

+0

你能告訴我,改變的單選按鈕狀態的代碼? –

+0

我仍然沒有實現。但我只使用該庫。 – mack

+0

@mack你的問題兄弟 是什麼? –

回答

1

爲你的男的女的按鈕,我創建你必須做的國家你的自我怎麼一回事,因爲我沒有時間了點。我忙於工作。

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

var gender : NSString = "" 
@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" 
      }else{ 
       sender.selected = true; 
       btn_male.selected = false; 
       btn_female.selected = true; 
       gender = "2" 
      } 
      break; 
     case 20: 
      //female select 
      if (sender.selected == true) { 
       sender.selected = false; 
       btn_female.selected = true; 

       btn_male.selected = false; 
       gender = "2" 
      }else{ 
       sender.selected = true; 
       btn_female.selected = false; 
       btn_male.selected = true; 
       gender = "1" 
      } 

      break; 
     default: 
      break; 
     } 
    } 


override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(true) 
    if profileData.Gender .isEmpty { 

    }else{ 
     if profileData.Gender == "1"{ 
      self.btn_male.selected = true 
     }else{ 
      self.btn_female.selected = true 
     } 

    } 
} 

var data:[String:String]{ 

    return ["FirstName":firstName.textVal,"LastName":lastName.textVal,"DateOfBirth":dateOfBirth.textVal,"Email":emailId.textVal,"Mobile":mobile.textVal,"Gender":gender as String,"Country":country.textVal,"State":state.textVal,"City":city.textVal] 
} 

在你的代碼

@IBAction func addUserBtnPressed(sender: AnyObject) { 


      profileData.FirstName = self.data["FirstName"]! 
      profileData.LastName = self.data["LastName"]! 
      profileData.DateOfBirth = self.data["DateOfBirth"]! 
      profileData.Email = self.data["Email"]! 
      profileData.Mobile = self.data["Mobile"]! 

      if btn_male.selected == true { 
       profileData.Gender = "1" 
      }else{ 
       profileData.Gender = "2" 
      } 

      profileData.Country = self.data["Country"]! 
      profileData.State = self.data["State"]! 
      profileData.City = self.data["City"]! 


      let toViewController = NextVC 
      toViewController!.transitioningDelegate = self.transitionManager 
      presentViewController(toViewController!, animated: true, completion: nil) 

    } 

設計

+0

嗨,謝謝你的更新。但是,我如何改變圖像? – mack

+0

按鈕單擊並將所選圖像設置爲故事板。 –

+0

我叮囑你。我是否需要在故事板中更改該圖像。關於使用代碼,我應該需要從故事板更改 – mack