Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call an AppDelegate method from RootViewController without passing a delegate?

how to call an AppDelegate method from RootViewController without passing a delegate?

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

like image 543
Greg Avatar asked Apr 21 '11 23:04

Greg


People also ask

Which method of AppDelegate is called first in iOS?

AppDelegate. UIViewController is allocated as the window's initial view controller. To make the window visible, makeKeyAndVisible method is called.

What is UIApplication shared delegate?

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. Like the UIApplication object, UIKit creates your app delegate object early in your app's launch cycle so it's always present.

Is AppDelegate a controller?

The application delegate is a controller object. By default, it is the owner and controller of the main window -- which is a view -- in an iOS app. The app delegate receives messages from an object representing -- or modeling -- the application itself (an instance of UIApplication ).


2 Answers

[[[UIApplication sharedApplication] delegate] someMethod];

Works like a charm!

like image 129
Daniel Amitay Avatar answered Oct 12 '22 23:10

Daniel Amitay


You can get access to the app delegate from any controller using

MyDelegate* aDelegate = (MyDelegate *)[[UIApplication sharedApplication] delegate];
like image 26
Cory Powers Avatar answered Oct 13 '22 01:10

Cory Powers