我创建了一个注册页面(ViewController),在性别选择上,我使用UISegmentedControl来表示“男性”、“女性”、“冷漠”。但是如何从UISegmentedControl中获取值并放入字典中呢?我使用自定义Dictionary<String, String> = [:]进行注册,但是UISegmentedControl不是字符串(显然),我得到错误:
无法将“UISegmentedControl”类型的值赋给“String”类型
如何将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")
        }
    }
}

最佳答案

代码中有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
}

关于ios - 从选择的UISegmentedControl段获取字符串值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54174368/

10-13 04:19