根据另一个视图控制器更改viewController BG颜色 [英] Change viewController BG color based off another view controller

查看:85
本文介绍了根据另一个视图控制器更改viewController BG颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要一个项目的帮助。我有一个简单的标签栏SVC,其中第一个视图是计时器,第二个视图是设置页面。在设置页面上,我为结构设置了颜色数组,然后,当用户单击按钮时,就会调用该数组中的随机颜色并将其应用于背景。这部分按我的预期工作。然后,我想将该颜色应用于第二个视图的背景。

So I need some help for a project. I have a simple tab bar SVC where the first view is a timer and the second is a settings page. On the settings page I've setup a struct with an array of colors, then when a user clicks a button a random color in the array is called and applied to the back ground. This part works just as I Intended. What I'd like to do is then apply that color to the background of the second view.

这里是设置代码

import UIKit
import GameKit

public var randomColor = UIColor()

class SettingsViewController: UIViewController {


@IBOutlet weak var pushMe: UIButton!
let colorProvider = BackgroundColorProvider()

@IBAction func pushMeChange(_ sender: Any) {
randomColor = colorProvider.randomColorBG()
print (randomColor.superclass as Any)
view.backgroundColor = randomColor
}

struct BackgroundColorProvider {
let colors = [
    UIColor(red: 90/255.0, green: 187/255.0, blue: 181/255.0, alpha: 1.0), // teal color
    UIColor(red: 222/255.0, green: 171/255.0, blue: 66/255.0, alpha: 1.0), // yellow color
    UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0), // red color
    UIColor(red: 239/255.0, green: 130/255.0, blue: 100/255.0, alpha: 1.0), // orange color
    UIColor(red: 77/255.0, green: 75/255.0, blue: 82/255.0, alpha: 1.0), // dark color
    UIColor(red: 105/255.0, green: 94/255.0, blue: 133/255.0, alpha: 1.0), // purple color
    UIColor(red: 85/255.0, green: 176/255.0, blue: 112/255.0, alpha: 1.0), // green color
]

func randomColorBG() -> UIColor {
let randomNumber = GKRandomSource.sharedRandom().nextInt(upperBound: colors.count)
return colors[randomNumber]
    }
}

}

然后我在这里从主视图控制器中获取了它:
更改背景颜色一个视图控制器中项目中的所有视图?

Then I have this in the main viewController I pulled from here: Changing background color of all views in project from one view controller?

该函数执行以下错误不会出错,但是我充其量只是个菜鸟,不知道铃应该如何工作,我怀疑它甚至被呼叫了。

The function does error below doesn't error out however I'm a noob at best, I'm not sure how the bell should work and i doubt its even being called. Any help is appreciated.

// bringing in background color from SettingsViewController

 func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "Load View") {
        // pass data to next view
        let viewController:SettingsViewController = segue!.destination as!  SettingsViewController
        viewController.view.backgroundColor = self.view.backgroundColor
    }
}


推荐答案

Swift具有称为单个,可用于管理视图控制器列表中的设置。在计时器视图的 viewDidLoad()方法中,您可以将视图的背景色设置为 singleton.backgroundColor 。在设置视图中,当用户选择新颜色时,应设置 singleton.backgroundColor = newColorThatUserChose 。这样,当用户切换回计时器视图时,颜色将自动切换。

Swift has this neat feature called a singleton that can be used to manage "settings" across a list of view controllers. In the viewDidLoad() method of the timer view you can set the view's background color to singleton.backgroundColor. In the settings view, when the user selects a new color, it should set singleton.backgroundColor = newColorThatUserChose. This way, when the user switches back to the Timer View, the color will automatically switch.

如上面的链接所示,可以这样创建一个单例:

As shown in the link above, a singleton can be created like this:

class Settings {
    static let sharedInstance = Settings()
    var backgroundColor = UIColor.White // set to white by default.
}

然后在计时器视图的viewDidLoad方法中:

Then in the viewDidLoad method for the Timer View:

self.view.backgroundColor = Settings.sharedInstance.backgroundColor

最后,当用户选择新颜色时,在SettingsViewController中:

Finally in the SettingsViewController when the user chooses a new color:

var color = [UIColor.White, UIColor.Black, UIColor.Blue, UIColor.Green........]
Settings.sharedInstance.backgroundColor = color[x] // where x is the index that was chosen.

这将允许视图根据应用程序设置自动更改。为了确保在您要更改颜色的所有视图中都能正常工作,

This will allow the views to change automatically based on the apps settings. To ensure that this works in all of the views that you would like to change the color for,

self.view.backgroundColor = Settings.sharedInstance.backgroundColor

应放置在每个 UIViewController

要进一步抽象,可以创建一个名为 GeneralUIViewController 的自定义类。键入 UIViewController 并在 viewDidLoad 方法中包含以上代码。完成此操作后,每个UIViewController的类都应设置为 GeneralUIViewController 。这样一来,您只需要在一个文件中设置背景色,项目中的每个视图控制器就会自动继承其背景色设置为用户在此应用程序的设置页面中选择的背景色。

To further abstract this, you can create a custom class called GeneralUIViewController which is a class of type UIViewController and has the above code in its viewDidLoad method. After doing this every UIViewController should have its class set to GeneralUIViewController. This will make it so you only need to set the background color in one file and every view controller in your project will automatically inherit setting its background color to what the User has chosen in the settings page of this application.

重新打开应用程序时,应该保存这些首选项,以便可以将CoreData用于此目的。我会查看此链接以获取更多信息。

These preferences should probably be saved when the application is reopened so CoreData can be used for this. I'd check out this link for more information.

这篇关于根据另一个视图控制器更改viewController BG颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆