VC中动态改变背景颜色

Posted

技术标签:

【中文标题】VC中动态改变背景颜色【英文标题】:Dynamically changing background color in VC 【发布时间】:2021-11-23 21:52:12 【问题描述】:

我的作业被这块卡住了。也许有人可以解释我如何解决它。“VC 包含一个带有内置控制器的控制器。两者都是父级和子控制器有三个按钮:绿色、黄色、紫罗兰。当您单击父控制器中的一个按钮时,子控制器的背景颜色会更改为相应的颜色。当您单击子控制器中的按钮时,父级的背景发生变化。”

这是我在代码中所做的 父 VC

import UIKit

class OrangeViewController: UIViewController, VioletControllerDelegate 
   
    

    var delegate: VioletControllerDelegate?
    
    override func viewDidLoad() 
        super.viewDidLoad()
        
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
            let vcV = segue.destination as! VioletViewController
            vcV.bgColor = view.backgroundColor
            vcV.delegate = self
        

    
    @IBAction func greenOButton(_ sender: Any) 
        //TO DO
    
    
    @IBAction func yellowOButton(_ sender: Any) 
        //TO DO
    
    
    @IBAction func purpleOButton(_ sender: Any) 
        //TO DO
    
    
    func setColor(color: String) 
        view.backgroundColor = UIColor(named: color)
    

儿童 VC

import UIKit
protocol VioletControllerDelegate
    func setColor ( color : String)


class VioletViewController: UIViewController 
    var bgColor : UIColor?
   
    var delegate: VioletControllerDelegate?
    
    override func viewDidLoad() 
        super.viewDidLoad()
        var bgColor = view.backgroundColor
    
    
    @IBAction func setGreen(_ sender: Any) 
        delegate?.setColor( color: "green")
    
    @IBAction func setYellow(_ sender: Any) 
        delegate?.setColor( color: "yellow")
    
    
    @IBAction func setViolet(_ sender: Any) 
        delegate?.setColor( color: "violet")
    
    
    func setColor ( color : String)
    view.backgroundColor = UIColor(named: color)
    

谢谢!

【问题讨论】:

【参考方案1】:

我做到了。如果有人需要这样的东西,我会发布我的答案:

父 VC

import UIKit

class OrangeViewController: UIViewController, VioletControllerDelegate 
   
   
    

    var childVC: VioletViewController?
    
    override func viewDidLoad() 
        super.viewDidLoad()
        
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
            let vcV = segue.destination as! VioletViewController
            self.childVC = vcV
            vcV.delegate = self
        

    
    @IBAction func greenOButton(_ sender: Any) 
        childVC?.view.backgroundColor = UIColor.green
    
    
    @IBAction func yellowOButton(_ sender: Any) 
        childVC?.view.backgroundColor = UIColor.yellow
    
    
    @IBAction func purpleOButton(_ sender: Any) 
        childVC?.view.backgroundColor = UIColor.purple
    
    
    func setColor( color : UIColor) 
        view.backgroundColor = color
    

儿童 VC

import UIKit
protocol VioletControllerDelegate
    func setColor ( color : UIColor)


class VioletViewController: UIViewController 
    
   
    var delegate: VioletControllerDelegate?
    
    override func viewDidLoad() 
        super.viewDidLoad()
        
    
    
    @IBAction func setGreen(_ sender: Any) 
        delegate?.setColor( color: UIColor.green)
    
    @IBAction func setYellow(_ sender: Any) 
        delegate?.setColor( color: UIColor.yellow)
    
    
    @IBAction func setViolet(_ sender: Any) 
        delegate?.setColor( color: UIColor.purple)
    
    
    

【讨论】:

以上是关于VC中动态改变背景颜色的主要内容,如果未能解决你的问题,请参考以下文章

VC改变CListCtrl 表格中文字颜色,和背景颜色。

VC++如何改变按钮背景色

如何动态改变JFrame的背景颜色?

easyUI的datagrid,怎么动态改变某一行的背景颜色

用vue动态改变body背景

s-s-rS 动态改变单元格背景和字体颜色