Why does AppDelegate inherit from UIResponder?

余生颓废 提交于 2019-11-29 02:52:15

问题


I noticed that when creating a new project with the iPhone Master-Detail template in Xcode 4.2 beta 4, it does:

// AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate>

Why does AppDelegate inherit from UIResponder instead of NSObject?


回答1:


From Converting to Storyboards Release Notes:

Note: In the current Xcode templates, 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. If you haven’t made use of this pattern in an existing application, there’s no need to adopt it for storyboards.




回答2:


Check the documentation of UIResponder. Since AppDelegate can respond to touch events, it implements the UIResponder interface.




回答3:


UIResponder is the base class for the UIKit framework. UIResponder can deal with the events.

Your AppDelegate class is the delegate class for the UIApplication which UIApplicationMain creates. AppDelegate conforms to the UIApplicationDelegate protocol.

The UIResponder class has the methods to get the window of the application focus on which all the views will be populated, so you should have a class that inherits from UIResponder in order to make the window as key.




回答4:


I would guess it is so it has access to the global undo manager.




回答5:


AppDelegate inherits from UIResponder that handles iOS events. The UIResponder class defines an interface for objects that respond to and handle events.




回答6:


UPDATE: My answer below might be wrong. I was just going by the image in the iOS documentation. But, it must be outdated.

Unless there's something new in iOS 5 and not yet documented, then I think this is a typo with this Xcode 4.2 beta 4 template. In an iOS app, the app delegate should subclass NSObject, not UIResponder, e.g.:

@interface AppDelegate : NSObject <UIApplicationDelegate>

For iOS apps, in UIKit (for Cocoa Touch, e.g., iPhone & iPad), UIApplication is the last responder in the responder chain.

For Mac OS X apps, in the Application Kit (for Cocoa, e.g., Mac), the app delegate is the last responder in the responder chain.



来源:https://stackoverflow.com/questions/6893221/why-does-appdelegate-inherit-from-uiresponder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!