How to call AppDelegate method from RootViewController without passing delegate?

How to call the AppDelegate method from RootViewController without passing delegate?

Just wondering if there is a way to do this? (or I need to create a delegate object in the RootViewController to store a reference to AppDelegate)

+6
source share
3 answers
[[[UIApplication sharedApplication] delegate] someMethod]; 

It works like a charm!

+7
source

You can access the application delegate from any controller using

 MyDelegate* aDelegate = (MyDelegate *)[[UIApplication sharedApplication] delegate]; 
+6
source

This happens so often that I always add the MyCustomAppDelegate method for this for me (so I don't have a lot of castings in my code.

 @implementation MyCustomAppDelegate () - (MyCustomAppDelegate *)appDelegate { return (MyCustomAppDelegate *)[[UIApplication sharedApplication] delegate]; } @end 

now i can call

 [MyCustomAppDelgate appDelegate] 
+2
source

Source: https://habr.com/ru/post/886432/


All Articles