Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When use [[UIApplication sharedApplication] delegate]

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!!

like image 269
rasputin Avatar asked Nov 25 '10 13:11

rasputin


People also ask

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.

What is app delegate in iOS Swift?

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.

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.


1 Answers

[[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.

  1. when you declare MyView *delegate this should either be id delegate or YourAppNameAppDelegate *delegate.

  2. 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.

like image 53
Kenny Winker Avatar answered Sep 22 '22 23:09

Kenny Winker