How to access a property value of AppDelegate class from someView Controller without creating reference of the delegate in view controller?
Asked
Active
Viewed 2.0k times
3 Answers
52
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@synthesize
d 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.

Tim
- 59,527
- 19
- 156
- 165
-
One more question [UIApplication sharedApplication].delegate; Is this a standrad way , why Am asking is I need a property called "sharedObject" in 3 different class files , "sharedObject" resides in AppDelegate class ? – Sabha B Mar 03 '11 at 02:39
-
1Unless you want to create a singleton class to house the property, using `[[UIApplication sharedApplication] delegate]` is the standard way to access that property among multiple classes. (And let's be honest; you're basically accessing a singleton anyway. No need to multiply singletons.) – Tim Mar 03 '11 at 03:06
20
[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;

par
- 17,361
- 4
- 65
- 80
2
[UIApplication sharedApplication].delegate;

madmik3
- 6,975
- 3
- 38
- 60
-
Am looking for alternative way to achieve the same besides doing [UIApplication sharedApplication].delegate;. 1. Something like accessing the value through publish/subscribe? 2. One more question [UIApplication sharedApplication].delegate; Is this a standrad , way why Am asking is I need property called "sharedObject" in 3 different class files , "sharedObject" resides in AppDelegate class ? – Sabha B Mar 03 '11 at 02:37