如何将 UIApplicationDelegate 添加到 UIResponder 链的末尾?
我试图为应用程序各个部分所需的常见功能提供一个 IBAction
方法。
即登录是以模态方式实现的,如果成功会产生一个通知,允许所有加载的视图控制器对此事件做出反应(从匿名转换到经过身份验证),
@interface MyAppDelegate : NSObject <UIApplicationDelegate>
{
...
}
- (IBAction)loginTapped:(id)sender;
@end
我将该按钮的操作设置为 IB 中的第一响应者,但是响应者链不给 MyAppDelegate
响应的机会。
我的问题是,我不想在响应者链的各个部分复制该方法,我想将其添加到已经是子类的公共类中。
UIResponder
链一直延伸到 UIApplication
但这似乎就结束了。我的 UIApplicationDelegate
无法参与。
我想将我的应用程序委托插入或添加到响应者链!
(或者找到另一种方法将 UIButton
touch-up-inside 连接到应用程序范围内的实现。我想避免子类化 UITabBarController
或 UIWindow
如果可能的话)
I'm trying to provide an IBAction
method for common functionality that is required at various parts of the app.
i.e. Login is implemented modally, and if it succeeds results in a notification that allows all loaded view controllers to react to this event (transition from anonymous to authenticated)
@interface MyAppDelegate : NSObject <UIApplicationDelegate>
{
...
}
- (IBAction)loginTapped:(id)sender;
@end
I set the action of that button to the First Responder in IB, however the responder chain doesn't give MyAppDelegate
a chance to respond.
My problem is that, I don't want to replicate the method at various parts of the responder chain, I would like to add it to a common class that is already a subclass.
The UIResponder
chain goes all the way through to the UIApplication
but that seems to be the end. My UIApplicationDelegate
doesn't get to participate.
I want to insert or add my app delegate to the responder chain!
(or find another way to hook up the UIButton
touch-up-inside to an app-wide implementation.. I want to avoid subclassing the UITabBarController
or UIWindow
if possible)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以对
UIApplication
进行子类化,让-nextResponder
返回应用程序委托(如果它是UIResponder
子类)。然后,您需要更改对UIApplicationMain
的调用以使用您的自定义子类。我自己没有尝试过,但我想不出这会导致任何直接问题。
You could subclass
UIApplication
to have-nextResponder
return the application delegate if it is aUIResponder
subclass. You'll then need to alter your call toUIApplicationMain
to use your custom subclass.I haven't tried this myself, but I can't think of any immediate problems this would cause.
从 iOS 5 开始,AppDelegate 自动成为响应者链中的最后一个链接。 Xcode 项目模板将创建一个 AppDelegate,它是
UIResponder
的子类,而不是NSObject
的子类。但是,为了避免 AppDelegate 混乱,请重写 AppDelegate 的nextResponder
并将工作卸载到自定义UIResponder
对象中:Starting with iOS 5, the AppDelegate is automatically the last link in the responder chain. Xcode project templates will create an AppDelegate that is a subclass of
UIResponder
rather thanNSObject
. However, to avoid cluttering up the AppDelegate, override the AppDelegate'snextResponder
and offload work into a customUIResponder
object: