0
class SecondViewController:UIViewController {
  required init(coder aDecoder: NSCoder){
    super.init(coder: aDecoder)
    //Custom logic here
  }
}

Quite a newbie question:

a view controller(SecondViewController), inherent from UIViewController needs a designated init function like above.

In this case, how should I call it, given I am not sure what the value for "coder" should be? I used to call the controller as: SecondViewController(), but it gives:

Missing argument for parameter 'coder' in call

I understand coder parameter has to be provided, but want to ask what its value comes from.

1 Answer 1

7

Thanks for the answers from @Chackle. Finally the solution I figured out is below.

What I want:

  • Inherit my SecondViewController from UIViewController
  • Simply to initialize any new SecondViewController as SecondViewController()

Solution:

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    init() {
        super.init(nibName: nil, bundle: nil)
        //Do whatever you want here
    }

"required init(coder aDecoder: NSCoder)" is a must if you create a subclass of UIViewController. And so is "super.init(nibName: nil, bundle: nil)", because it is the way how UIViewController does initialization.

Sign up to request clarification or add additional context in comments.

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.