0

i have sended a color name with a delegate from a different ViewController and the data recieves successfully(printed the right color). Then if I want to change the backroundcolor of the view itself then with that name that I received but it didn't work.

func sendColor(color: String) {
    let colorLowercased = color.lowercased()
    print(colorLowercased)

    view.backgroundColor = UIColor(named: "\(colorLowercased)")
    print(view.backgroundColor)
}

and the code execute

after changing the backgroundcolor is nil and the view is black

in the colorviewController here's my delegate function to send

@IBAction func colorPressed(_ sender: UIButton) {
    let selectedColor = sender.currentTitle

    if let delegate = delegate {
        delegate.sendColor(color: selectedColor!)
    }

    _ = navigationController?.popViewController(animated: true)

}

btw im using swift 4.2

swwiftii
  • 27
  • 3

1 Answers1

1

It is black because your code is trying to set view.backgroundColor with a color which is likely nil. Do you have named colors in your app asset? To add them you can refer to this post . Alternatively, instead of passing title to your sendColor: pass the color of your button (title color/bgColor/tintColor).

func sendColor(color: UIColor)

janusfidel
  • 8,036
  • 4
  • 30
  • 53