在Swift上更改选择器视图的颜色

时间:2017-12-09 11:57:30

标签: swift uipickerview

我的viewController上有5个选择器视图。我想改变我的所有选择器视图的颜色。我做了一切,但我没能成功。它默认为黑色。我想把它变成白色。我怎样才能解决这个问题。任何人都可以帮我解决我的代码吗?此外,在用户从pickerview输入一些数据后,如果他或她首先返回页面然后再次进入此屏幕,数据将保留他或她进入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
        }
    }    
}

1 个答案:

答案 0 :(得分:1)

我认为您的要求可以通过简单地使用此委​​托方法来实现

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