Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 201
Sabha B Avatar asked Mar 03 '11 02:03

Sabha B


People also ask

What is the use of AppDelegate in iOS?

Your app delegate object manages your app's shared behaviors. The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system.


1 Answers

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:

  1. Reference the application delegate, casting as appropriate. You would write code in your view controller class like:
    id propertyValue = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] myProperty];
  2. 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.

like image 86
Tim Avatar answered Oct 01 '22 18:10

Tim