简体   繁体   中英

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. 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. 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.
}

Then in the viewDidLoad method for the Timer View:

self.view.backgroundColor = Settings.sharedInstance.backgroundColor

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

should be placed in every 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM