Create singleton of a viewcontroller in swift 3

Poles picture Poles · Dec 14, 2016 · Viewed 8.7k times · Source

I know how to create singleton class in swift. The best and easy way to create singleton class is the following:

class Singleton {
    static let sharedInstance = Singleton()
}

But I don't need singleton for any normal class. I need to create singleton for a viewcontroller class. So I'm using this code create singleton

class AViewController:UIViewController {

    static let sharedInstance = AViewController()

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

}

it gives me error near AViewController()

Missing argument for parameter 'coder' in call

Looks like it want me to initialize with init(coder: NSCoder). But what parameter or value should I pass through the coder?

Answer

Rob picture Rob · Dec 14, 2016

If you really wanted to have singleton for a view controller corresponding to some scene, you'd probably do something like:

class SecondViewController: UIViewController {

    static let shared = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Foo")

}

In this example, the storyboard was Main.storyboard and the storyboard identifier for the scene in question was Foo. Obviously, replace those values for whatever was appropriate in your case.

Then your other view controller that was invoking this could do something like:

@IBAction func didTapButton(_ sender: Any) {
    let controller = SecondViewController.shared
    show(controller, sender: self)
}

I wouldn't recommend singletons for view controllers. View controllers (and their views) should be created when needed and be allowed to be deallocated when they're dismissed. And you're losing many storyboard benefits (by which you see the logical flow between scenes with segues between them). And, if you use this view controller in different contexts, you're inviting problems stemming from the view controller hierarchy falling out of sync with the view hierarchy. I really would discourage you from using singletons for view controllers.

But if you were going to do it, you could do something like that...