1

There is already a question like this that was answered, however, I was not satisfied with the answer: Why does AppDelegate inherit from UIResponder?.

I'd like more specifics, as I am trying to understand the architecture of an iOS app.

As far as I know, UIResponder is for things that "respond" to external forces (i.e. touch events, motion events, etc.). How come the initialization of an application needs to be a UIResponder? Maybe I'm not understanding what the instance of AppDelegate does.

Community
  • 1
  • 1
Fine Man
  • 455
  • 4
  • 17
  • I think I now understand what the AppDelegate does: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html?hl=ar. However, I'm still not sure why AppDelegate inherits from UIResponder. – Fine Man Apr 02 '16 at 20:11
  • 2
    Note that external events can be of several types, not only physical interactions. For instance, responding to background modes (going to background, stopping audio when other apps need it, etc.) are exacly the things that `UIResponder` is supposed to manage. Following the delegate pattern, the app itself (I mean, `UIApplication`) is supposed to manage it, but it delegates to an appropiate object (the app delegate). – Alejandro Iván Apr 02 '16 at 20:16
  • Thanks, @AlejandroIván. So, let me clarify (with lack of appropriate terminology): UIResponder spans the OS, while the UIApplication is specific to each app? Please correct me if I'm wrong. – Fine Man Apr 02 '16 at 20:28
  • 1
    Let me say it another way: the user taps an icon on the OS, which instantiates an `UIApplication` (the app you tapped). That `UIApplication` is an object that needs to do several things, including responding to input. As the app object itself would respond differently (depending on what the programmer wants it to do), it "delegates" the work to the app delegate. That delegate, of course, needs to follow a protocol to communicate with the app itself, so it inherits from `UIResponder` class, that has the majority of methods implemented (and you override what your specific app needs). – Alejandro Iván Apr 02 '16 at 20:33
  • @AlejandroIván, thanks for the clarification. Where could I learn all this stuff? I understand that the casual iOS Developer might not care too much about the details, but I would like to know exactly what's going on under-the-hood. – Fine Man Apr 02 '16 at 20:38
  • 1
    I'm not sure about an official source (the docs are extremely verbose, which confuses everyone). I would recommend you to read this, it should be clarifying: http://oleb.net/blog/2011/06/app-launch-sequence-ios/ – Alejandro Iván Apr 02 '16 at 20:41
  • @AlejandroIván, thanks for the source. If I have more questions, I've been warned to move this discussion to a chat, so we can continue there. – Fine Man Apr 02 '16 at 20:49
  • Or you could ask a new question. I believe your original question should be already answered, or am I wrong? – Alejandro Iván Apr 02 '16 at 20:51
  • @AlejandroIván, Yes, I suppose you are correct. – Fine Man Apr 02 '16 at 20:52
  • Is there a chat room that you use so I can ask for help? My question may be redundant asking (but I don't quite fully understand it), so I'd rather do it via chat rather than making a new question. – Fine Man Apr 11 '16 at 01:02

1 Answers1

3

The application delegate class inherits from UIResponder.

This is so that the delegate instance can participate in the responder chain and so handle application-level actions.

Edit:

As of iOS 5 there is a step 6: The app delegate gets the final word on events. Beginning with iOS 5 the app delegate inherits from UIResponder and no longer from NSObject as it did before.

In short: first the view, if the view has a view controller then that, next the superview until the top of the hierarchy, the window. From there to the application and finally to the app delegate.

The addition of the app delegate as potential responder is a welcome addition since we rarely – if ever – subclass UIApplication or UIWindow. But we always have our own app delegate if only to create the window and add the rootViewController in the application:didFinishLaunching… delegate method. So this happens to be the de facto best place for a responder to fall back to if there is none in the entire view hierarchy.

Taken from:

https://www.cocoanetics.com/2012/09/the-amazing-responder-chain/

Witterquick
  • 6,048
  • 3
  • 26
  • 50