如何将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到一个应用程序范围的实现..我想避免如果可能的话UITabBarController
或UIWindow
)
如果是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; }