I don't know when or why I should use [[UIApplication sharedApplication] delegate]
I use delegate when [self.navigationController pushViewController:myView Animation:YES]
does't navigate. I make MyView *delegate = [[UIApplication sharedApplication] delegate]
and [delegate pushViewController:myView Animation:YES]
to make the navigation work correctly.
I don't know if the solution is correct and in addition I don't know what is the use for [[UIApplication sharedApplication] delegate]
or what are its effects on the application.
Can someone help me?
Thanks!!
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.
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.
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.
[[UIApplication sharedApplication] delegate]
will give you a pointer to your app delegate. the one that was automatically created when you made the project. Usually it's named YourAppNameAppDelegate
.
two things are a little wrong with your code.
when you declare MyView *delegate
this should either be id delegate
or YourAppNameAppDelegate *delegate
.
You shouldn't have to access your navigationController via the app delegate. I would look into why self.navigationController is failing, and address that. You're basically implementing a singleton pattern when you rely on the app delegate. Because the singleton pattern is the least cool programming design pattern, it won't win you any points when talking shop with other programmers.
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