1

I have a class called UIViewControllerModel which I like to initialize backgroundColor for this class, I am sure this a syntax error, need help for correction please.

class UIViewControllerModel: UIViewController {
    
    var backgroundColor: UIColor 

    required init(backgroundColor: UIColor) {

        super.init()
        self.backgroundColor = backgroundColor

    }

    override func viewDidLoad() {
        
        super.viewDidLoad()
        view.backgroundColor = backgroundColor
 
    }

}

enter image description here

ios coder
  • 1
  • 4
  • 31
  • 91

2 Answers2

4

The required initializer is not the right one -- because your class is a subclass of UIViewController, you need a required init?(coder: NSCoder). You can put your custom initializer that sets backgroundColor in separate init.

Also, instead of viewDidLoad, use loadView for your custom View Controllers that you make in code. This is how you do it:

class UIViewControllerModel: UIViewController {
    
    var backgroundColor: UIColor

    /// Put your custom argument labels here, not inside the `required init?`
    init(backgroundColor: UIColor) {
        self.backgroundColor = backgroundColor
        super.init(nibName: nil, bundle: nil)
    }
    
    /// This is in case the View Controller is loaded from the Storyboard
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// Use this instead of viewDidLoad
    override func loadView() {
        
        /**
         Instantiate the base `view`.
         */
        view = UIView()
        view.backgroundColor = backgroundColor
        
    }

}
let modelVC = UIViewControllerModel(backgroundColor: UIColor.blue)
self.present(modelVC, animated: true)

Result:

View controller with blue background presented
aheze
  • 24,434
  • 8
  • 68
  • 125
  • 2
    could you please explain or update the answer why I should use **loadView** instead **viewDidLoad**? because both working same for me – ios coder Feb 11 '21 at 22:55
  • 1
    @swiftPunk `loadView` actually *creates* the view. If you don't override it, the [documentation](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621454-loadview) says that a "plain UIView object" will be created instead. Although this is exactly what I did in with `view = UIView()`, you might need to change it to something else, like `view = UIImageView()`. Meanwhile `viewDidLoad` is called after the view is already created. That is mainly used when the view controller is loaded from the storyboard. See this [answer](https://stackoverflow.com/a/573968/14351818) – aheze Feb 11 '21 at 23:05
  • @aheze I'm doing the exact same thing as you, but I'm getting `Replace 'backgroundColor' with 'coder'` – Jalil Nov 05 '21 at 19:05
  • @Jalil I just double checked and works fine for me. Maybe [ask a new question](https://stackoverflow.com/questions/ask)? – aheze Nov 05 '21 at 19:10
2

Try this

class UIViewControllerModel: UIViewController {

 var backgroundColor: UIColor 

 init(backgroundColor: UIColor) {
     self.backgroundColor = backgroundColor
     super.init(nibName: nil, bundle: nil)

 }

 required init?(coder: NSCoder) {
  fatalError("init(coder:) has not been implemented")
 }

 override func loadView() {

  self.view.backgroundColor = backgroundColor

 }

}
SuperTully
  • 319
  • 1
  • 6