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

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

roottraveller
  • 7,942
  • 7
  • 60
  • 65
Jared Chu
  • 2,757
  • 4
  • 27
  • 38
  • Why do you want to do that? It's just that you may think doing in the good way, but there could be an alternative. So why do you want to do that? – Larme Oct 11 '16 at 07:54
  • I'm developing a music app which has to call a PlayerViewController from multiple OtherViewController (example: https://github.com/jaredchu/MusicPlayerTransition). Do you have any idea? Thanks. – Jared Chu Oct 11 '16 at 08:00
  • A sharedInstance could be interesting. If you want to show it always (available in each ViewController), you may be interested on how does SWRevealViewController or others stuff like it, keeps their panel always visible, etc. – Larme Oct 11 '16 at 08:03
  • I will go research for SharedInstance. Thanks for your time and your answer. – Jared Chu Oct 11 '16 at 08:08
  • Check my answer . and tell me its helpful for you or not ? – Himanshu Moradiya Oct 11 '16 at 09:15

2 Answers2

0

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]

Community
  • 1
  • 1
0

Objective C

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
someViewController *afv=[storyboard instantiateViewControllerWithIdentifier:@"someViewController"];
afv.getdata();
[self.sideMenuViewController setContentViewController:afv animated:YES];

Swift

let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let someobject: someViewController = (mainStoryboard.instantiateViewControllerWithIdentifier("someViewController") as! someViewController)
someobject.getdata()
self.navigationController?.pushViewController(someobject, animated: true)
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49