Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a reference to the UIApplication delegate

I'm writing my first iPhone application and I'm having trouble switching views. I have 2 views and a reference to each in the AppDelegate (an instance of UIApplicationDelegate). I create instances of both in the applicationDidFinishLaunching and immediately show the first view. This works fine.

The problem is the reference to the other view is in the AppDelegate and I can't figure out how to get a reference to it so I can switch to the other view. Is there a way to get a reference to the main UIApplication or UIApplicationDelegate objects?

like image 345
derGral Avatar asked Jul 26 '09 09:07

derGral


People also ask

What is UIApplication 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.

What is the difference between AppDelegate and SceneDelegate?

AppDelegate is responsible for handling application-level events, like app launch and the SceneDelegate is responsible for scene lifecycle events like scene creation, destruction and state restoration of a UISceneSession.

What is the AppDelegate in Xcode?

So an app delegate is an object that the application object can use to do certain things like display the first window or view when the app starts up, handle outside notifications or save data when the app goes into the background.


2 Answers

Yes, UIApplication is a singleton, and uses the normal singleton pattern for Objective-C:

[UIApplication sharedApplication]; 

You can get your delegate class directly from it:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; 
like image 148
Louis Gerbarg Avatar answered Sep 21 '22 11:09

Louis Gerbarg


Use:

[[UIApplication sharedApplication] delegate]; 
like image 38
Codebeef Avatar answered Sep 20 '22 11:09

Codebeef