0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Hyder
  • 139
  • 1
  • 10
  • Pet peeve, but nonetheless important - you do not "show" a `UIViewController`, you show it's root view (or `UIView`). Could you be more specific about your issue? Maybe include some code? Thanks. –  Mar 20 '18 at 19:13
  • You could create it at startup as a property of your app delegate and then ask for it as needed. Somewhat cleaner would be to create a separate factory class that builds the controller when it's first requested, then saves it for later requests. – Phillip Mills Mar 20 '18 at 19:16

1 Answers1

1

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

Akshansh Thakur
  • 5,163
  • 2
  • 21
  • 38