如何将 UIApplicationDelegate 添加到 UIResponder 链的末尾?

发布于 2024-09-12 05:24:13 字数 700 浏览 2 评论 0原文

我试图为应用程序各个部分所需的常见功能提供一个 IBAction 方法。

即登录是以模态方式实现的,如果成功会产生一个通知,允许所有加载的视图控制器对此事件做出反应(从匿名转换到经过身份验证),

@interface MyAppDelegate : NSObject <UIApplicationDelegate>
{
  ...
}
- (IBAction)loginTapped:(id)sender;
@end

我将该按钮的操作设置为 IB 中的第一响应者,但是响应者链不给 MyAppDelegate 响应的机会。

我的问题是,我不想在响应者链的各个部分复制该方法,我想将其添加到已经是子类的公共类中。

UIResponder 链一直延伸到 UIApplication 但这似乎就结束了。我的 UIApplicationDelegate 无法参与。

我想将我的应用程序委托插入或添加到响应者链!

(或者找到另一种方法将 UIButton touch-up-inside 连接到应用程序范围内的实现。我想避免子类化 UITabBarControllerUIWindow 如果可能的话)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So要识趣 2024-09-19 05:24:13

您可以对 UIApplication 进行子类化,让 -nextResponder 返回应用程序委托(如果它是 UIResponder 子类)。然后,您需要更改对 UIApplicationMain 的调用以使用您的自定义子类。

我自己没有尝试过,但我想不出这会导致任何直接问题。

You could subclass UIApplication to have -nextResponder return the application delegate if it is a UIResponder subclass. You'll then need to alter your call to UIApplicationMain to use your custom subclass.

I haven't tried this myself, but I can't think of any immediate problems this would cause.

虚拟世界 2024-09-19 05:24:13

从 iOS 5 开始,AppDelegate 自动成为响应者链中的最后一个链接。 Xcode 项目模板将创建一个 AppDelegate,它是 UIResponder 的子类,而不是 NSObject 的子类。但是,为了避免 AppDelegate 混乱,请重写 AppDelegate 的 nextResponder 并将工作卸载到自定义 UIResponder 对象中:

- (UIResponder *)nextResponder
{
    static dispatch_once_t onceToken;
    static LastUIResponder *lastResponder = nil;
    dispatch_once(&onceToken, ^{
        lastResponder = [[LastUIResponder alloc] init];
    });
    return lastResponder;
}

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 than NSObject. However, to avoid cluttering up the AppDelegate, override the AppDelegate's nextResponder and offload work into a custom UIResponder object:

- (UIResponder *)nextResponder
{
    static dispatch_once_t onceToken;
    static LastUIResponder *lastResponder = nil;
    dispatch_once(&onceToken, ^{
        lastResponder = [[LastUIResponder alloc] init];
    });
    return lastResponder;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文