2

I created an App using a storyboard in Xcode. the App contains an UITabBarController (TBC) and some UIViewControllers (VC), controlled from the TBC.

In the AppDelegate a create an object (lets call it "myMidi") which will listen to incoming MIDI-Messages (CoreMidi). So I implement the interfaces of this myMidi-Object in my VC. I connected the views to the TBC using the "Relationship" option in the Interface Builder (IB).

All VCs, created in the IB are a instance of my own VC Class. As i mentioned bevore, in this Class I implement the interface of the the myMidi-Object I created in the AppDelegate. So every VC-instance holds an address of an myMidi-Object and need now the one and only reference which was created in AppDelegate.

So I am wondering how can I access to the current VC or the TBC which is displayed currently to pass this reference to my VC Class?

Or how else can I solve this issue. I guess I should do this somewhere in AppDelegate or should I hold the reference in the TBC too and pass it to every new VC when the VC will be created !?

C4 - Travis
  • 4,502
  • 4
  • 31
  • 56

2 Answers2

1

If I understood correctly, you want your MyMidi class's current (only?) instance to be available to your VCs?

If you only ever have one Instance, consider making MyMidi a singleton class and give it a class method à la + (MyMidi *)sharedInstance; which always returns (and lazily initializes) the same object. That way, you can access it from anywhere.

See this question on how to implement a singleton in Cocoa/Cocoa Touch.

Community
  • 1
  • 1
fzwo
  • 9,842
  • 3
  • 37
  • 57
  • Thank you for the answer, I give the singleton a try. – user1366036 May 02 '12 at 13:54
  • There is a difference between **needing only one** instance and **ensuring only one** instance can be created, generally the former is preferable e.g. don't use a singleton unless you need to ensure only one instance can be created... – Paul.s May 02 '12 at 14:05
0

Use nsdefaults setObjec:forKey: to pass the object and get it where you want. Or make a property in appdelegate and access it through

AppDelegate* appDelegate = (TappAppDelegate*) [[UIApplication sharedApplication] delegate];
appDelegate.object = yourObject;
MPelletier
  • 16,256
  • 15
  • 86
  • 137
Saad
  • 8,857
  • 2
  • 41
  • 51