Reputation: 2089
How to access a property value of AppDelegate class from someView Controller without creating reference of the delegate in view controller?
Upvotes: 25
Views: 19680
Reputation: 60150
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:
id propertyValue = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] myProperty];
@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.
Upvotes: 52
Reputation: 17734
[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;
Upvotes: 20