Jared Chu Jared Chu - 1 year ago 85
Objective-C Question

Call the same view controller from many other view controller programmatically without using any segue

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController")

Run code above in multiple view controller they will init multiple
. How to call the same
instance in this case?

Answer Source

I think this is what you need to use for that effect Using a dispatch_once singleton model in Swift More specific: Swift :

 class SomeViewController {
      class var sharedInstance: SomeViewController {
        struct Static {
          static let instance: SomeViewController = UIStoryboard(name: "MyStoryboardName", bundle: nil).instantiateViewControllerWithIdentifier("someViewController")
        return Static.instance

Objective C:

    + (instancetype)sharedInstance; // add this to SomeViewController.h file
    + (instancetype)sharedInstance { // and this in SomeViewController.m
       static SomeViewController *instance = nil;

       if (!instance) {
          UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboardName" bundle:nil];
          instance = [storyboard instantiateViewControllerWithIdentifier:@"SomeViewController"];

       return instance;

then use with [SomeViewController sharedInstance]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download