2

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

4
  • 1
    What does the error say? Commented Feb 11, 2021 at 22:33
  • updated on top. Commented Feb 11, 2021 at 22:45
  • Side comment: please see How to Ask. Especially the part where it says in bold: "DO NOT post images of code, data, error messages, etc.". Images of text are not searchable. Commented Feb 12, 2021 at 10:26
  • thanks, I was carful about codes, but I did know about errors Commented Feb 12, 2021 at 10:34

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

4 Comments

could you please explain or update the answer why I should use loadView instead viewDidLoad? because both working same for me
@swiftPunk loadView actually creates the view. If you don't override it, the documentation 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
@aheze I'm doing the exact same thing as you, but I'm getting Replace 'backgroundColor' with 'coder'
@Jalil I just double checked and works fine for me. Maybe ask a new question?
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

 }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.