[英]Change viewController BG color based off another view controller
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.
我有一个简单的标签栏SVC,其中第一个视图是计时器,第二个是设置页面。 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.
我想做的是然后将该颜色应用于第二个视图的背景。
Here is the settings code 这是设置代码
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 has this neat feature called a singleton that can be used to manage "settings" across a list of view controllers. Swift有一个称为单例的简洁功能,可用于管理视图控制器列表中的“设置”。 In the
viewDidLoad()
method of the timer view you can set the view's background color to singleton.backgroundColor
. 在计时器视图的
viewDidLoad()
方法中,您可以将视图的背景颜色设置为singleton.backgroundColor
。 In the settings view, when the user selects a new color, it should set singleton.backgroundColor = newColorThatUserChose
. 在设置视图中,当用户选择新颜色时,应设置
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.
}
Then in the viewDidLoad method for the Timer View: 然后在Timer View的viewDidLoad方法中:
self.view.backgroundColor = Settings.sharedInstance.backgroundColor
Finally in the SettingsViewController when the user chooses a new color: 最后在SettingsViewController中,当用户选择一种新颜色时:
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
should be placed in every UIViewController
. 应放在每个
UIViewController
。
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. 为了进一步抽象,你可以创建一个名为
GeneralUIViewController
的自定义类,它是一个类型为UIViewController
的类,并在其viewDidLoad
方法中包含上述代码。 After doing this every UIViewController should have its class set to GeneralUIViewController
. 执行此操作后,每个UIViewController都应将其类设置为
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. 这将使您只需要在一个文件中设置背景颜色,项目中的每个视图控制器都将自动继承将其背景颜色设置为用户在此应用程序的设置页面中选择的颜色。
These preferences should probably be saved when the application is reopened so CoreData can be used for this. 应用程序重新打开时,可能应保存这些首选项,以便可以使用CoreData。 I'd check out this link for more information.
我会查看此链接以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.