Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

importing AppDelegate

Often times I initialize my model class variables in my AppDelegate so they can be used by different ViewControllers without passing their instance across class files. However, every time I import AppDelegate into a .m file to access these variable's data I feel like I'm doing some wrong.

Is this the the correct way for accessing these variables or should I be doing something differently?

EDIT: My problem isn't how to access the variables. I currently use this line of code to get my appDelegate instance:

id appDelegate = [[UIApplication sharedApplication] delegate];

Conceptually, I want to know if this is the accepted way to interact with an application's model classes. It seems, to me, that an application's AppDelegate manages the application overall. So it seems counterintuitive to import this class into other classes further down an application's class chain.

like image 468
J Max Avatar asked Dec 07 '11 19:12

J Max


People also ask

What is the AppDelegate file?

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 AppDelegate in Objective C?

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.

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.


1 Answers

Is this the the correct way for accessing these variables or should I be doing something differently?

You'll find that different people have different opinions about this. The style that I prefer is to have the app delegate pass the necessary information to the first view controller, and have that view controller pass it on to whatever view controllers it creates, and so on. The main reason for this is that it prevents child view controllers from depending on things that they have no business knowing about.

If you have some detail editor, for example, you want to be able to pass that editor exactly what it needs to do its work. If you give it that information, the editor is completely flexible -- it'll edit any information that you give it. If the editor knows that it should get its data from some external object, like the app delegate, then it loses some degree of flexibility -- it can only get data from the thing that it knows about.

So, it's fine to set up your data model in the app delegate. But when it comes to providing access to the model, think: tell, don't ask. That is, have the app delegate tell the first view controller what model object to use, and have that controller tell the next one, and so on. If you have to ask, you have to know who to ask, and that's where the dependencies start heading in the wrong direction.

every time I import AppDelegate into a .m file to access these variable's data I feel like I'm doing some wrong.

Trust that instinct. Think about why it feels wrong.

like image 65
Caleb Avatar answered Sep 21 '22 17:09

Caleb