从选择的UISegmentedControl段获取字符串值

时间:2019-01-14 00:05:47

标签: ios swift uisegmentedcontrol

我创建了一个SignUp页面(ViewController),在选择性别时,我使用的是“男”,“女”,“无所谓”的UISegmentedControl。但是,如何从UISegmentedControl中获取值并放入字典中呢?我使用自定义Dictionary<String, String> = [:]进行注册,但是UISegmentedControl不是字符串(显然),并且出现错误:

  

无法将类型为“ UISegmentedControl”的值分配为“字符串”

如何转换UISegmentedControl的结果以接收这些值?

import UIKit
import FirebaseAuth
import Firebase

class SignUpViewController: UIViewController{
    @IBOutlet weak var nameField: UITextField!
    @IBOutlet weak var emailField: UITextField!
    @IBOutlet weak var birthdayField: UITextField!
    @IBOutlet weak var passwordField: UITextField!
    @IBOutlet weak var confirmpasswordField: UITextField!
    @IBOutlet weak var instagramuserField: UITextField!
    @IBOutlet weak var generoSegmentControl: UISegmentedControl!
    @IBOutlet weak var DatePickerDate: UITextField!
    @IBOutlet weak var signupOutlet: UIButton!

    @IBAction func signupAction(_ sender: Any) {
        if let name = self.nameField.text {
            if let email = self.emailField.text {
                if let birthday = self.birthdayField.text {
                    if let password = self.passwordField.text {
                        if let confirmpassword = self.confirmpasswordField.text {
                            if let instagramuser = self.instagramuserField.text {
                                if let gender = self.generoSegmentControl {

                                    if password == confirmpassword {
                                        print("Senhas iguais, podemos seguir")
                                    }else {
                                        print("As senhas precisam ser iguais")
                                    }

                                    self.auth.createUser(withEmail: email, password: password) { (user, error) in

                                        if error == nil {

                                            var user: Dictionary<String, String> = [:]

                                            user["nome"] = name
                                            user["email"] =  email
                                            user["nascimento"] =  birthday
                                            user["instagram"] =  instagramuser
                                            user["genero"] = gender



                                            //Encoding email for Base 64
                                            let key = Base64().encodingStringBase64(text: email)

                                            let users = self.database.reference().child("usuarios")

                                            users.child(key).setValue(user)

                                            print("Sucesso ao cadastrar usuário!")
                                        }else{
                                            print("Erro ao cadastrar usuário, tente novamente!")
                                        }
                                    }
                                }else {
                                    print("Escolha seu genero")
                                }
                            }else{
                                print("O campo usuário do instagram esta vazio")
                            }
                        }else{
                            print("As senhas não conferem")
                        }
                    }else{
                        print("Digite uma senha")
                    }
                }else{
                    print("Digite a data do seu nascimento")
                }
            }else{
                print("Digite seu e-mail")
            }
        }else{
            print("Digite seu nome")
        }
    }

    @IBAction func generoAction(_ sender: Any) {
        let genIndex = generoSegmentControl.selectedSegmentIndex

        switch genIndex {
        case 0:
            print("Homem")
        case 1:
            print("Mulher")
        case 2:
            print("Indiferente")
        default:
            print("Nada selecionado")
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您在代码中获得了segmentedControl,但不是selectedSegmentIndex属性。然后,在接收到selectedSegmentIndex之后,您应该从控件中获取该索引的标题。使用以下命令更新代码:

if let gender = self.generoSegmentControl {

将其更改为:

let genderIndex = self.generoSegmentControl.selectedSegmentIndex
if let gender = self.generoSegmentControl.titleForSegment(at: genderIndex) {

}

或更改更新字典的方式:

let index = gender.selectedSegmentIndex
if let segmentTitle = gender.titleForSegment(at: index) {
    user["nome"] = name
    user["email"] =  email
    user["nascimento"] =  birthday
    user["instagram"] =  instagramuser
    user["genero"] = segmentTitle
}