Why not subclasses of UIApplication? Why use a delegate?

Instead of using an application delegate so that UIApplication singleton can call delegate methods at a predefined time, what are the advantages and disadvantages of simply subclassing UIApplication ? ( update:) why does the iOS architecture use the application delegate instead of letting people write the application by subclassing UIApplication and overriding its various methods?)

This is because when we create a new UIView control, we are a subclass of UIView ( update: or we are also a subclass of UIViewController ). So, why in the case of the application we are not subclasses of UIApplication , but instead use delegation?

+4
source share
5 answers

Again, from the documentation:

Subclass Notes

You can choose a subclass of UIApplication to override sendEvent: or sendAction: to: from: forEvent: implement custom events and dispatch actions. However, there is rarely a legitimate need to extend this class; application delegate (enough UIApplicationDelegate for most cases. If you are subclassing UIApplication, be sure that you are trying to execute with a subclass.

+6
source

From the Link to the UIApplication Class:

The UIApplication class provides a centralized point of control and coordination for applications running on iOS.

Each application must have exactly one instance of UIApplication (or a subclass of UIApplication). When the application starts, the UIApplicationMain function is called; among other tasks, this function creates a singleton UIApplication object. After that, you can access this object by calling the method of the sharedApplication class.

So what do you mean by β€œwhy aren't we subclassing UIApplication? Apple even provides notes on what to implement in subclasses.

As for your question about delegating and using a single-point network only for standard classes, the answer is simple: the application should provide one common way to receive and send events (both external and system-related), multitask processing and interface using (therefore main.m contains a link to the delegate of your application).

+2
source

The reason developers usually don't do this is because there is no need. UIApplication works as most cases require. He just needs some clues about what to do in certain predefined cases (so he has a delegate). UIView, on the other hand, is very general, and I don’t think it is ever used as is. This is probably the most customizable class in the world of iOS.

+2
source

Delegation is a basic design pattern. This allows you to share responsibilities between parts of your program. The idea is that part of your program, which, for example, draws on the screen, probably should not talk to your database. There are several reasons for this:

Performance: if the same object that accesses the screen accesses your data warehouse, you are faced with performance issues. Period. Full stop.

Code Rationale: It is easier to conceptualize code that is properly modular. (For me, anyway)

Flexibility: if you are a subclass in your code, this is great - until you start working in monolithic classes that have all kinds of undesirable behavior. You will reach the point where you have to overload the behavior to disable the action, and the property namespace may become dirty. Try categories, delegation, and blocks for alternatives.

However, I did encounter a situation where a subclass was appropriate. I have a kiosk app in which I wanted to automatically reject a specific settings menu if it has not interacted with it for a certain period of time. To do this, I had to have access to touch events throughout the application. I subclassed UIApplication and redid sendEvent: Then it was appropriate, although this is an extreme case. As King Solomon says in Ecclesiastes, to paraphrase: there is time and place for everything under the sun.

To make writing, reading, repeating and maintaining your program easier, it is highly recommended that you follow certain practices. You can subclass many classes, and Apple will not give up your application for bad code, provided that it works as advertised. However, if you do not follow specific proven practices, you dig your own grave. The subclass is not inherently bad, but the categories, protocols and blocks are so fascinating that I would prefer them anyway.

+2
source

There are many special things that a UIView subclass might need. Think about UIScrollView, UIImageView, UIWebView, etc. And how much they should be different. Nevertheless, they must participate in the hierarchy of representations, and therefore subclasses make sense.

UIApplication, on the other hand, is the central hub for application events, notifications, opening URLs, window access, and other common things. Under normal circumstances, an application should really just know what the UIApplicationDelegate Protocol will be.

A note from UIApplication Overview explains why you can subclass UIApplication:

You may decide to subclass UIApplication to override sendEvent: or sendAction: to: from: forEvent: to implement custom dispatch of events and actions. However, there is rarely a need to extend this class; application delegate (UIApplicationDelegate is sufficient for most cases). If you are performing a subclass of UIApplication, be very sure that you are trying to execute with a subclass.

But this is only necessary in special cases.

+1
source

Source: https://habr.com/ru/post/1414493/


All Articles