user141302
user141302

Reputation:

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

Upvotes: 0

Views: 1739

Answers (3)

Saud Alfadhli
Saud Alfadhli

Reputation: 856

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!

Upvotes: 0

luvieere
luvieere

Reputation: 37494

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

Upvotes: 0

Bryan Henry
Bryan Henry

Reputation: 8408

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.

Upvotes: 2

Related Questions