Brandon Slezak
Brandon Slezak

Reputation: 187

Why do I keep having to instantiate appdelegate every time I need to use it?

Currently I use this code, when I need a local method level instance of app delegate:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

Isn't there some sort of easier "global" way of accessing this?

In other languages like C# you can create public static classes and just say things like...

if (ObjectHelper.isLoggedin)
{
   //Do Work
}

However I'm finding it kind of frustrating in objective-c when I want to use app delegate, i have to:

Import it in the header, Instantiate a local instance of it, Actually get a global state property I need out of it

Before you say "Don't use app delegate", even if i created my own static class, and I wanted ONE instance of it for the shared app i would STILL have to put my own static class in app delegate if I wanted to share it between view controllers, right?!?!?!

What am I missing, this seems lame...., how do you manage global constants, settings, etc in an easier way? Or do you just have to import/create the app delegate reference EVERY SINGLE TIME???

thanks!!!

Upvotes: 2

Views: 1111

Answers (4)

Midhun MP
Midhun MP

Reputation: 107211

The AppDelegate is a singleton.

You are not initializing it each time, you are calling the static method that returns the singleton object.

For meeting your requirement, you can use the following methods:

1. You can use #define to define a macro shortcut like:

#define AppDelegate (AppDelegate *)[[UIApplication sharedApplication] delegate]

2. You can define a static method like:

+ (AppDelegate *)getAppDelegate
{
   return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

Upvotes: 1

jnix
jnix

Reputation: 480

#define kSingleton                            static id shared; static dispatch_once_t done; dispatch_once(&done, ^{shared = [self new];}); return shared;
  • (id)sharedManager {kSingleton} // method for creating singletone instance

Create class 'Manager' as singletone class use this class to access global data any where in your application.

And most important add this Manager.h in -prefix.h. This will make sure this class will be available throughout your application. Its easy and clean way as per my thinking

Hope it helps !!!

Upvotes: 0

Rajesh
Rajesh

Reputation: 10434

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

This line does not mean you are creating a local instance. You are simply accessing the reference of delegate instance of [UIApplication sharedApplication] singleton.

Upvotes: 1

Ryan
Ryan

Reputation: 3993

Well, first, let's clear up some misconceptions.

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

This is not instantiating anything. Let's break this down a bit.

[UIApplication sharedApplication]

This is a call to a class method on UIApplication. UIApplication returns an already-existing value of type UIApplication* that was created indirectly from your main.m on app launch. Let's call the result of this call "a":

UIApplication *a = [UIApplication sharedApplication]

Now let's look at the next layer.

[a delegate]

This is effectively what you're calling, I've just broken it out to make it more clear. You are accessing the delegate property on an instance of UIApplication. This returns an already-assigned pointer to a class that implements the UIApplicationDelegate protocol. In this case, it is your class, AppDelegate, and this was allocated directly in your main.m's main() function. (Go look, you should see a macro there that includes this!)

Now, to solve your problem.

You essentially want access to this instance of the AppDelegate class from anywhere in your project. This requires two things. First, any files that want's to talk to an AppDelegate* must import AppDelegate.h. Second, if you want a shorter property to access, you need to either allocate this somewhere, or assign it on a global scale.

For the first problem, you can simply import "AppDelegate.h in your precompiled header file. Search your project for this ".pch" file. This is included at the top of every .m when your code is compiled.

Add the following line to it:

#import "AppDelegate.h"

Now, everyone in your project can see this!

To solve the second part, I have a better solution. Since the original call to the sharedApplication works so well, let's just #define a shortcut! In that same .pch file, add this:

#define theAppDelegate (AppDelegate *)[[UIApplication sharedApplication] delegate]

Now, anywhere in your app, you can just call theAppDelegate to get access. Now, whether this is smart is debatable, but you said you want this power.

Upvotes: 7

Related Questions