sorin
sorin

Reputation: 170758

How do I access the object that implements UIApplicationDelegate from anywhere in my code?

What is the proper way of accessing a method of my application (object implementing UIApplicationDelegate) from anywhere in my code?

Upvotes: 2

Views: 2610

Answers (2)

Tobias
Tobias

Reputation: 4397

Caleb is right, adding a tip below that saves on typing and increases symmetry between AppKit and UIKit.

If you're a Mac programmer you're probably used to accessing the delegate like so,

   [NSApp delegate]

In AppKit, NSApp is defined so,

   #define NSApp [NSApplication sharedApplication]

So to get something similar in iOS do this,

   #define UIApp [UIApplication sharedApplication]

Then to access the delegate you would do this:

   [UIApp delegate]

Upvotes: 6

Caleb
Caleb

Reputation: 125017

id<UIApplicationDelegate> delegate = [[UIApplication sharedApplication] delegate];

Upvotes: 3

Related Questions