tags:

views:

379

answers:

2

hi, 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.......?

A: 

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.

Sbrocket
I'd be interested to know why people are voting this down. This answer is fairly trivial but a reference to the object he wants to message is exactly what he needs. A class method and static variables is not the answer for accessing instance variables - that's not even remotely possible.
Sbrocket
A: 

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].

luvieere
A class method doesn't get him anywhere. He can't access instance variables of a specific instance, in that case, and since he wants "to get data value from view controller page", he needs to message a specific instance of that class.
Sbrocket
I won't get into an argument with you, but the problem that you raise is not something that cannot be worked around. I know this first hand, as I use class methods in conjunction to static variables to access shared instances and objects. The details of such an access method are not hard to infer, and while it might not be the best technique to use, it is certainly quite easy to implement and use.
luvieere
A class method and static variables is not the answer for accessing a specific instance of said class - that's doesn't make sense nor is it possible.
Sbrocket