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?
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.
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.
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.
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];
Use:
[[UIApplication sharedApplication] delegate];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With