0

I created this class to configure every view controller to avoid redundancy inside viewdidload().

class Configuration: UIViewController  {

    func setNavigationTheme(#backgroundImage: String, dashboardImage: String) {

        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: dashboardImage), forBarMetrics: UIBarMetrics.Default)
        self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
        let bgImage: UIImage = UIImage(named: backgroundImage)!
        self.view.backgroundColor = UIColor(patternImage: bgImage)

    }

}

inside viewDidLoad()

var configure = Configuration()
configure.setNavigationTheme(backgroundImage: "Background", dashboardImage: "Dashboard")

When I call the function, it does not change anything, am I doing it wrong or not?

shaideru
  • 45
  • 1
  • 6

1 Answers1

1

You need to extend your view controller with your Configuration class.

    class MyViewController: Configuration {

    override func viewDidLoad() {
        //Don't initialize new Configuration class.
        setNavigationTheme(backgroundImage: "Background", dashboardImage: "Dashboard")

    }
}

Your Configuration class:

class Configuration: UITableViewController  {

    func setNavigationTheme(#backgroundImage: String, dashboardImage: String) {

        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: dashboardImage), forBarMetrics: UIBarMetrics.Default)
        self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
        let bgImage: UIImage = UIImage(named: backgroundImage)!
        self.view.backgroundColor = UIColor(patternImage: bgImage)

    }

}
gellezzz
  • 1,165
  • 2
  • 12
  • 21
  • how do I fit that here "class ActivityTableViewController: UITableViewController " should I instead create a protocol? – shaideru Feb 23 '15 at 15:27
  • Just change your Configuration class with "class Configuration: UITableViewController {...)" instead of "class Configuration: UIViewController {...}" I edited my answer – gellezzz Feb 23 '15 at 15:37
  • it worked! but how am I going to apply this to a UIViewController and other controllers? – shaideru Feb 23 '15 at 16:11
  • I believe there's a better way to do this – shaideru Feb 23 '15 at 16:37
  • Create own helper class with static function and pass backgroundImage, dashboardImage and navigationController. [link](http://stackoverflow.com/questions/24107833/class-level-or-struct-level-method-in-swift-like-static-method-in-java) – gellezzz Feb 23 '15 at 16:44