How can I pass referece from an object in AppDelgate to ViewControllers - iphone

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 !?

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.

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;

Related

How to make a delegate object accessible throughout a ViewController

I'm trying to get an instance of my AppDelegate accessible to all methods in each ViewController that I have. If I try to declare it with other class variables I get Initializer Element is not a compile-time constant. If I declare it in a method within the ViewController however I am able to use it. I am trying to save integers and floats to properties I have set up in the AppDelegate (a big no-no I know, but this is a project for an introductory class and I'm not expected to get too advanced, especially since everything we've done so far is not compliant with the MVC paradigm). The app uses a toolbar to switch between views using the app's ViewController to load the other ViewControllers as subviews. I was going to put the AppDelegate declaration and update statements in the ViewDidUnload method of each view controller, but I'm not sure that the Views are unloaded whenever they are switched (they're switched by removing the current View from the SuperView and loading the new one as a Subview at index 0). What happens to the views that are not currently being viewed then? Is there a method that detects that that I could implement the AppDelegate declaration and updates into?
I guess ideally I'd like to be able to access the AppDelegate object in any method in my ViewControllers because I have a lot of quantities being updated throughout and would like to have those quantities updated in the AppDelegates values as soon as they happen, since I'm not sure what happens with a View is cleared from SuperView
Thanks in advance everyone
You can access your app delegate via [[UIApplication sharedApplication] delegate] from anywhere in your application.
You should never instantiate another copy of the object on your own. The system does this for you at startup.
As for detecting changes, you can override the viewDidDisappear method of UIViewController. (You're correct--in general, they will not be unloaded when switched, and viewDidUnload will not be called)

passing data back to a previously allocated UIViewController

I have a view controller that then has a button that passes to an option menu.
When options are set they need to be past back to the previously allocated viewcontroller.
How is this possible without 'alloc and init another' instance of the object?
You can achieve this by using a delegate protocol. First view controller should become the delegate of the second view controller and then you can call this delegate method in your first view controller once the selection is done.
You can lookup google for implementation of delegates in objective-c. Its pretty simple. Add a
#protocol <delegatename>
<declare delegate method>
#end
Create a member variable in the second view controller for assigning the delegate. And define the method in the class implementing the delegate.
When you init your option viewController, pass it a reference to its parent.
I.E.
[[OptionViewController alloc] initWith...: parent:];
Use a #property or a method or somesuch to call on the parent to pass the data back.
You could use a shared singleton?
http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
Or save the parameters to nsuserdefaults and read them back in in your first viewcontroller
or some other temporary store such as your appdelegate
I've used all three of the above approaches before.

How to access one UIViewControllers properties from another UIViewController?

I have one single MainViewController which has of course it's one main UIView. I have this main view comprised of many different subviews.
Some of these subviews have their very own ViewController.
Lets say the MAIN view (whose delegate is primarily MainViewController) has a container which loads another UIView that uses a separate UIViewController- SecondaryViewController as the delegate for most it's actions.
This container view is of course loaded in MainViewController via
MyContainerViewController *myContainerController =
[[MyContainerViewController alloc] ...];
[self addSubView: myContainerController.view];
the controller for myContainerController.view though is MyContainerViewController. How inside this controller do I access MainViewController properties? Specifically I need to access MainViewController's - self.navigationController property to push a new ViewController? :)
Does this make any sense? I assume there's going to be casting of some sort involved since it seems I need to somehow retain a reference to MainViewController inside SecondaryViewController?
It doesn't make sense to push a new ViewController from the SecondaryViewController in the MainViewController.
This screws up the design of the code. A child object will access its parents method to call a method. By other words: bad code.
Make a delegate call from the SecondaryViewController to the MainViewController that it state has changed. Then the MainViewController can decide to do with the call and the SecondaryViewController will not know anything about the implementation of the MainViewController.
So:
Make a protocol in SecondaryViewController.
Let the MainViewController be SecondaryViewController's delegate.
Implement the delegate method in MainViewController which pushes the new ViewController.
Expose the desired sub-view controllers as properties of the view controller that contains them.
Expose your root view controller(s) as properties of your app delegate, and synthesize them also.
When you want to access a different UIViewController, first obtain a reference to your appDelegate:
MyAppDelegate* myAppDelegate = [[UIApplication sharedApplication] delegate];
Then just use your chain of properties to get access to your desired view controller.
SubSubViewController* ssvc = myAppDelegate.rootViewController.subViewController.subSubViewController;
Some folks frown upon the use of the sharedApplication class method to obtain the reference to a delegate, but I've used it in multiple apps and not suffered for it (yet). The alternative is to pipe your appDelegate through your hierarchy of viewControllers.

How to share data globally among multiple view controllers

can anybody help me out please...
i have a huge object in a model. i made it as a single ton class and returning the object wn other calls.but the object is very big thats y the app is crasing. with out returning how to share the data globally and when to alloc the object and where to dealloc the object. i dont need all the data in object in a viewcontroller ..i need specific data to a view controller from that object...
Thanks.
You can store a pointer to it in your app delegate and retrieve it using
BlahAppDelegate* delegate = [[UIApplication sharedApplication] delegate];
id bigObject = delegate.bigObject;
Since the app delegate will outlive the view controllers, you shouldn't have to worry about retain and release for it.
A singleton class should work similarly, as long as the singleton instance method (that retrieves the one created instance) calls retain on the instance when before it returns it. When the view is dealloc-ed, make sure you call release on the instance.

how to get to appdelegate from viewcontrollers' value?

i know how to access appdelegate's value inside Viewcontroller like
YourDelegate *appDelegate = (YourDelegate *)[[UIApplication sharedApplication] delegate];
but i want simple method like this when i want to get value from viewcontroller to appdelegate(reverse order).....? any help...?
suppose if i have one method in appdelate. i want to get data value from view controller page,i want to use it in appdelegte.m file.......?
To address this question more generally...If you want to do anything with an object – send it a message (call a method on an object), access some property of an object, pass the object as a parameter to some other method – you need a reference to that object.
That means that, in your case, your AppDelegate needs a reference to the view controller you want to access some property of. If the view controller is allocated and initialized in your app delegate, this is as simple as storing a reference to said view controller in your delegate until you need to use it (using an instance variable or whatever). If it wasn't, then you need to do something else to get your app delegate a reference to the view controller – the steps to do this would depend on where and how the view controller was created. Without more specific details, I can't help you with those steps.
Model-View-Controller (MVC) Sidenote:
If you are following MVC design practices, a view controller (or any other controller class) is not the object that should be storing your state information or other application data. That job is supposed to be performed by a model object.
Make the method be a class method (declared with + (void) MyMethod: (int)myParameter) and call it from your app delegate like this: [MyOtherViewController MyMethod: myParameter].
This to call checkAppTheme method from AppDelegate.m :
[(AppDelegate *)[[UIApplication sharedApplication] delegate] checkAppTheme];
Don't miss to change (checkAppTheme) to your method in your AppDelegate.m
Good luck!