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

我试图提供一个IBAction方法,以在应用程序的各个部分所需的常见function。

即login是以模态方式实现的,并且如果它成功导致允许所有加载的视图控制器对该事件作出反应的通知(从匿名到authentication的转换)

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

我将该button的动作设置为IB中的First Responder,但响应者链不会给MyAppDelegate一个回应的机会。

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

UIResponder链一直到UIApplication但似乎是结束。 我的UIApplicationDelegate没有参与。

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

(或find另一种方法来连接UIButton touch-up-inside到一个应用程序范围的实现..我想避免如果可能的话UITabBarControllerUIWindow

如果是UIResponder子类,则可以-nextResponder UIApplication以使-nextResponder返回应用程序委托。 然后,您需要将您的调用更改为UIApplicationMain以使用您的自定义子类。

我自己并没有尝试过,但是我不能想到这会造成任何直接的问题。

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

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