在 Swift 中使用选择器视图更改 UIBackground

Change UIBackground with Picker View in Swift

我正在尝试使用选取器中提供的颜色更改 UIBackground。我的选择器工作正常并且可以打印选定的颜色,但是我的问题是通过按下 selectedColorButton 来让 selectedColor 更改 UIBackground。我以为我可以在 view.backgroundColor = UIColor. 的末尾传入 selectedColor,但我无法让它工作,我想它是错误的。

谢谢!

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
    
    let colors = ["Red", "Green", "Orange", "Yellow", "Pink", "Purple"]
    
    var selectedColor = ""

    
    @IBAction func selectedColorButton(_ sender: UIButton) {
        
    //    view.backgroundColor = UIColor.

    }
    
    @IBOutlet weak var pickerView: UIPickerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        pickerView.delegate = self
        pickerView.dataSource = self
        
}
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return colors.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return colors[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        return selectedColor = colors[row]

    }


}

如您所见,view.backgroundColor 需要一个 UIColor 类型的值,不幸的是您只有一个 String。没有直接转换,但您可以在此处采用两种方法:

  1. 运行 包含 UIColor 值的第二个数组

您在此处使用颜色数组显示选择器选项:

let colors = ["Red", "Green", "Orange", "Yellow", "Pink", "Purple"]

您可以用包含相同顺序的实际 UIColor 值的第二个数组替换该实现。现在,不是跟踪选定的名称,而是跟踪选定的索引

var selectedIndex = 0
let colorNames = ["Red", "Green", "Orange", "Yellow", "Pink", "Purple"]
let colors: [UIColor] = [.red, .green, .orange, .yellow, .pink, .purple]

使用 colorNames 显示您的选项,然后使用索引访问 colors

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

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return colorNames[row]
}

@IBAction func selectedColorButton(_ sender: UIButton) {
    view.backgroundColor = colors[selectedIndex]
}
  1. 为名称使用系统属性

我们可以使用颜色的可访问性名称,而不是单独的数组来存储名称。现在,我们只需要一个颜色数组,我们可以直接存储选择的颜色,不需要索引跟踪或名称选择

var selectedColor: UIColor? = nil
let colors: [UIColor] = [.red, .green, .orange, .yellow, .pink, .purple]

现在显示这些颜色选项并使用它们的 accessibilityName 属性作为名称

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    selectedColor = colors[row]
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return colors[row].accessibilityName
}

@IBAction func selectedColorButton(_ sender: UIButton) {
    if let color = selectedColor {
        view.backgroundColor = color
    }
}

第一种方法的优点是我们可以控制名称。第二种方法的优点是我们节省了额外信息的内存。您的选择!

您甚至可以尝试其他方法,例如使用枚举或元组,甚至您自己的 类。我建议您全部尝试!

主要问题是您有一个 Strings 颜色列表,但您需要 UIColors 才能用作背景颜色。我认为最好的方法是使用 Dictionary,这样每一行都有一个名称和一个 UIColor。

函数 selectedColorButton 没有做任何事情,也不需要它,所以我删除了它。

完整代码如下:

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
    
    let colors = [["name": "Red", "color": UIColor.red],
                  ["name": "Green", "color": UIColor.green],
                  ["name": "Orange", "color": UIColor.orange],
                  ["name": "Yellow", "color": UIColor.yellow],
                  ["name": "Pink", "color": UIColor.magenta],
                  ["name": "Purple", "color": UIColor.purple]]
    
    @IBOutlet weak var pickerView: UIPickerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        pickerView.delegate = self
        pickerView.dataSource = self
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return colors.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if let name = colors[row]["name"] as? String {
            return name
        }
        return nil
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if let color = colors[row]["color"] as? UIColor {
            view.backgroundColor = color
        }
    }
}