在viewWillAppear if if语句

时间:2017-06-28 12:47:12

标签: ios arrays swift uipickerview viewwillappear

我正在创建一个应用程序,其中有4个标签,最后一个标签用于更改为暗模式(更改标签颜色,BG颜色,键盘颜色和pickerView颜色)。功能是我改变颜色的地方。一切正常,除了pickerView没有改变颜色,所以我需要帮助。

override func viewWillAppear(_ animated: Bool) {

    if UserDefaults.standard.integer(forKey: "dark") == 1{

        self.view.backgroundColor = UIColor.darkGray
        UITextField.appearance().keyboardAppearance = .dark
        textField.backgroundColor = UIColor.white
        textField.textColor = UIColor.black
        textField2.textColor = UIColor.black
        textField2.backgroundColor = UIColor.white
        label.textColor = UIColor.white
        label.textColor = UIColor.white
        labelIN1.textColor = UIColor.white

        func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
            let titleData = list[row]
            let myTitle = NSAttributedString(string: titleData, attributes: [NSForegroundColorAttributeName:UIColor.black])
            return myTitle
        }


    }else{

        self.view.backgroundColor = UIColor.white
        UITextField.appearance().keyboardAppearance = .light
        textField.backgroundColor = UIColor.white
        textField.textColor = UIColor.black
        textField2.textColor = UIColor.black
        textField2.backgroundColor = UIColor.white
        label.textColor = UIColor.black
        label.textColor = UIColor.black
        labelIN1.textColor = UIColor.black

        func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
            let titleData = list[row]
            let myTitle = NSAttributedString(string: titleData, attributes: [NSForegroundColorAttributeName:UIColor.white])
            return myTitle
        }

    }

}

1 个答案:

答案 0 :(得分:1)

pickerView(_:attributedTitleForRow:forComponent:)委托中的this answer委托在viewWillAppear中被声明为嵌套函数的代码段中的问题,如果其范围应该是out out,如下所示:

override func viewWillAppear(_ animated: Bool) {

        if UserDefaults.standard.integer(forKey: "dark") == 1{

            self.view.backgroundColor = UIColor.darkGray
            UITextField.appearance().keyboardAppearance = .dark
            textField.backgroundColor = UIColor.white
            textField.textColor = UIColor.black
            textField2.textColor = UIColor.black
            textField2.backgroundColor = UIColor.white
            label.textColor = UIColor.white
            label.textColor = UIColor.white
            labelIN1.textColor = UIColor.white

        }else{

            self.view.backgroundColor = UIColor.white
            UITextField.appearance().keyboardAppearance = .light
            textField.backgroundColor = UIColor.white
            textField.textColor = UIColor.black
            textField2.textColor = UIColor.black
            textField2.backgroundColor = UIColor.white
            label.textColor = UIColor.black
            label.textColor = UIColor.black
            labelIN1.textColor = UIColor.black

        }
    }

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

        let titleData = list[row]

        if UserDefaults.standard.integer(forKey: "dark") == 1{

            let myTitle = NSAttributedString(string: titleData, attributes: [NSForegroundColorAttributeName:UIColor.white])
            return myTitle

        } else {

            let myTitle = NSAttributedString(string: titleData, attributes: [NSForegroundColorAttributeName:UIColor.black])
            return myTitle
        }
    }

,请务必执行:

  • class ViewController: UIViewController, UIPickerViewDelegate { /*...*/ }
  • pickerView.delegete = self

代码中的某处。

有关详细信息,您可能需要查看enter image description here