Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 446
sorin Avatar asked Jan 28 '26 16:01

sorin


2 Answers

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]
like image 91
Tobias Avatar answered Jan 31 '26 08:01

Tobias


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

like image 34
Caleb Avatar answered Jan 31 '26 08:01

Caleb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!