The main part of the application delegation class

What is the main point about putting variables and method signatures inside ApplicationDelegate.h in Objective-C? By doing this, are all these methods and variables considered by other view controller classes? Is that the point?

And also: is there only one application delegate class inside each project?

+2


a source to share


2 answers


"By doing this, all of these methods and variables are considered by other view controller classes? Is that the point?"

Of course, any class that imports the application controller header can access public properties and methods (and ivars), but this applies to any class, not just the application delegate.

It is quite common to put a few important objects and methods that are needed throughout the application in the application delegate. Thus, yes, it is often the reason why you see variables and methods defined in an application's delegation class.



It sometimes makes sense, but it can very quickly deteriorate in poor design very much . You need to be sure not to use the application delegate as a trash can, where you can simply throw away all the shared state and functionality in the application. I am working with some code where someone has done this right now and it is extremely difficult to change and use refactoring features.

This is just a version of the well-known problems of using global state for everything. In short, you should analyze the functionality and divide it into individual classes or groups of classes. Try to keep your application delegate as thin as possible!

EDIT: ... and read Matt Gallagher 's post on the matter.

+2


a source


The Application Delegate (and yes, there is only one per application) is the backbone of the controller layer in Cocoa and Cocoa Touch applications. Its main function is to customize the NS / UIApplication behavior under certain circumstances (for example, terminating an application, opening an application, etc.). It is also commonly used to assign delegates to other responders (such as table views, schemas, etc.).



In small applications, it will usually act as both a delegate and a dataSource for most interface elements, but as Felixis points out, this can degrade quickly if the application grows.

+3


a source







All Articles