how to get to appdelegate from viewcontrollers' value? - iphone

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!

Related

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

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;

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.

Iphone access a property value from AppDelegate

How to access a property value of AppDelegate class from someView Controller without creating reference of the delegate in view controller?
I'm not quite sure what you mean - there are multiple ways to get information from your application delegate into a view controller, and the phrase "without creating reference of the delegate" is unclear. Your options basically are:
Reference the application delegate, casting as appropriate. You would write code in your view controller class like: id propertyValue = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] myProperty];
Pass the property in when creating the view controller. This requires the view controller to have a #property declared and #synthesized for use, then you would have the app delegate just set the property on the view controller instance.
Neither of these options require that you retain a copy of your app's delegate as a #property, but the first does reference the delegate once.
[UIApplication sharedApplication].delegate
You'll also need to include the app delegate header file in your view controller and possibly typecast the delegate from id to your actual app delegate class.
#include "MyAppDelegate.h"
((MyAppDelegate *)[UIApplication sharedApplication].delegate).myProperty;
[UIApplication sharedApplication].delegate;

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 can I outsource view-change-operations from my view controller upon accelerations?

I have an view controller class. I want to do some things in my view when the accelerometer detects movement and calls the accelerometer:didAccelerate: method in the delegate object.
That delegate object is the problem here in my brain. Currently, my brain is freezed and I don't get it what would be better. Let me know what you think!
Solution 1)
In my view controller class I conform to the UIAccelerometerDelegate protocol, and implement that accelerometer:didAccelerate: method.
In the -applicationDidFinishLaunching: method of my AppDelegate class I set that view controller object up as the delegate for receiving method calls upon accelerations. I think that's not really good.
Solution 2)
I create a blank new object called AccelerationDelegate, conform to that UIAccelerometerDelegate protocol, implement that accelerometer:didAccelerate: method and in the -applicationDidFinishLaunching: method of my AppDelegate class I set that view controller object up as the delegate for receiving method calls upon accelerations.
But for solution 2 my brain got stuck a little bit! How would I access the view objects from my view controller inside that object?
The problem here is, that I have more than one view controller around. I use a tab bar controller to switch between them.
Any suggestions how I could get that right?
I agree that the second method is better. Are you looking to access just the currently selected tab view, or just a specific view in your app.
In any case, what I would do is to set up properties for your UITabViewController in your UIApplicationDelegate so that you can access it from the delegate (you can get the app delegate by calling [[UIApplication sharedApplication] delegate]). For example:
YourApplicationDelegate *appDelegate = (YourApplicationDelegate *)[[UIApplication sharedApplication] delegate];
FirstUIViewController *firstViewController = appDelegate.firstViewController;
[firstViewController doStuff];
where firstViewController is a property on your delegate.
If your acceleration is specific to one view controller, then it makes sense to have the view controller receive the information necessary to alter its own subviews. However, it might be better to set your view controller to be the delegate when the view appears, and set the delegate to null when it disappears. (Specifically, - (void) viewWillAppear: and - (void) viewWillDisappear:)