0

I have a viewcontroller with required init?(coder aDecoder: NSCoder) , i want to create instance in other class. here is my code

class ViewControllerB: UIViewController { 
@IBOutlet weak var tableview: UITableView!

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

I want to create instance for ViewControllerB in ViewControllerA.

2 Answers 2

1

You can fix it in a few ways.

Option 1

let viewController = ViewControllerB(nibName: nil, bundle: nil)

Option 2

Change your ViewControllerB like following.

class ViewControllerB: UIViewController {
    @IBOutlet weak var tableview: UITableView!

    convenience init () {
        self.init(nibName: nil, bundle: nil)
    }
} 

OR

class ViewControllerB: UIViewController {
    @IBOutlet weak var tableview: UITableView!

    required init(coder aDecoder: NSCoder) {
        fatalError("This class does not support NSCoding")
    }
    override init (frame : CGRect) {
        super.init(frame : frame)
    }
    convenience override init () {
        self.init(frame:CGRectZero)
    }
}

And now you can call

let viewController = ViewControllerB()
Sign up to request clarification or add additional context in comments.

Comments

0

Do like this

let viewController = ViewControllerB()

This will give the instance of ViewControllerB and then do what you want.

1 Comment

thanks for your quick response, actually it has required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }}, so your answer doesnt work

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.