Showing a view controller across the app

I am having one view controller which should be hide and show from everywhere in the app without initializing it again. So I just want to know that how can I achieve this. Like by adding that view controller as childView or by presenting it to navigation controller or anything else.

The idea is that the view controller can be shown or hide from any screen of the app.

1 answer

  • answered 2018-03-20 19:06 Akshansh Thakur

    You can make a view controller as a cocoa touch class... and you can add a xib to it.. once you design the interface for the view controller..

    You can make a singleton class and keep the shared instance like this:

    class YourViewController: UIViewController {
    
    static let sharedInstance = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourStoryBoardId")
    
    }
    

    To show this just do :

    func someFunc() {
    
     show(YourViewController.sharedInstance, sender: self)
    
    }
    

    I once did something like this in one of my apps.. i think its a standard approach.

    You could also see this for more info and source