我的viewController上有5个picker视图。我想改变我所有选择器视图的颜色。我什么都做了,但我不能成功。它默认为黑色。我想把它变成白色。我怎么能解决这个问题。有人能帮我修改密码吗?另外,在用户从pickerview输入一些数据后,如果他或她第一次返回到back page,然后再次返回此屏幕,则数据将保留他或她输入到pickerview的任何内容,在此过程之后数据将不会清除。我该怎么做?

class LoggedInVC: UIViewController, UIPickerViewDataSource,UIPickerViewDelegate{

    @IBOutlet weak var cinsiyetButton: UIButton!
    @IBOutlet weak var türButton: UIButton!
    @IBOutlet weak var ageButton: UIButton!
    @IBOutlet weak var colorBotton: UIButton!
    @IBOutlet weak var healthButton: UIButton!
    @IBOutlet weak var cinsiyetPicker: UIPickerView!
    @IBOutlet weak var turPicher: UIPickerView!
    @IBOutlet weak var agePicker: UIPickerView!
    @IBOutlet weak var colorPicker: UIPickerView!
    @IBOutlet weak var healthPicker: UIPickerView!

    let cinsiyet = ["Erkek","Dişi"]
    let tur = ["pug","terrier","dogo","doberman"]
    let color = ["siyah","beyaz","kahverangi","karışık"]
    let age = ["1","2","3","4","5"]
    let health = ["kısır","Kısır Değil"]

    override func viewDidLoad() {
        super.viewDidLoad()

        cinsiyetPicker.dataSource = self
        cinsiyetPicker.delegate = self
        turPicher.dataSource = self
        turPicher.delegate = self
        agePicker.dataSource = self
        agePicker.delegate = self
        colorPicker.dataSource = self
        colorPicker.delegate = self
        healthPicker.dataSource = self
        healthPicker.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func cinsiyetPressed(_ sender: Any) {
        cinsiyetPicker.isHidden = false
    }

    @IBAction func turPressed(_ sender: Any) {
        turPicher.isHidden = false

    }

    @IBAction func agePressed(_ sender: Any) {

        agePicker.isHidden = false
    }

    @IBAction func colorPressed(_ sender: Any) {

        colorPicker.isHidden = false

    }

    @IBAction func healthPressed(_ sender: Any) {
        healthPicker.isHidden = false
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView.tag==1{
            return cinsiyet.count
        }
        else if pickerView.tag==2{
            return tur.count
        }
        else if pickerView.tag==3{
            return age.count
        }
        else if pickerView.tag==4{
            return color.count
        }
        else if pickerView.tag==5 {
            return health.count
        }
        return health.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        if pickerView.tag==1{
            return cinsiyet[row]
        }
        else if pickerView.tag==2{
            return tur[row]
        }
        else if pickerView.tag==3{
            return age[row]
        }
        else if pickerView.tag==4{
            return color[row]
        }
        else if pickerView.tag==5 {
            return health[row]
        }
        return health[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        if pickerView.tag==1{
            cinsiyetButton.setTitle(cinsiyet[row], for: UIControlState())
            cinsiyetPicker.isHidden = true
        }
        if pickerView.tag==2{
            türButton.setTitle(tur[row], for: UIControlState())
            turPicher.isHidden = true
        }
        if pickerView.tag==3{
            ageButton.setTitle(age[row], for: UIControlState())
            agePicker.isHidden = true
        }
        if pickerView.tag==4{
            colorBotton.setTitle(color[row], for: UIControlState())
            colorPicker.isHidden = true
        }
        if pickerView.tag==5{
            healthButton.setTitle(health[row], for: UIControlState())
            healthPicker.isHidden = true
        }
    }
}

最佳答案

我认为你的要求可以通过简单地使用这个委托方法来实现

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString?

关于swift - 在Swift中更改uipickerview的颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47728344/

10-12 14:33