2017-10-13 76 views
0

无论如何要知道在AppDelegate中,新的UIViewController被推入,而无需在AppDelegate和UIViewController中调用任何额外的方法?当UIViewController转换到另一个UIViewController时,我怎么知道AppDelegate?

可以AppDelegate能够知道哪些新的UIViewController被推入,而没有写任何特定的UIViewController代码段?

+3

ViewControllers从导航控制器拥有的各自导航堆栈中被推入并弹出。 AppDelegate只是UIApplication对象生活事件的委托。他们根本没有关系你想达到什么? –

+1

真的没有调用任何额外的方法? –

回答

-2

有做不恰当的方式,但作为一种变通方法,你可以使用这个方法:

func application (_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { 
    guard let vc = (window?.rootViewController?.presentedViewController) else { 
     return .portrait // your default orientation, to update according to your needs 
    } 
    if (vc.isKind(of: NSClassFromString("The class you are looking to")!)) { 
// Here you can put any code you want, your view controller was just pushed 
     return .allButUpsideDown 
    } 
    return .portrait 
// return the orientation you want 
} 

的问题是,你需要做的为每个的viewController铸造在您的应用程序,imo做一些自定义代码会更干净,更适合您的需求。

+0

您的解决方案如何检测是否有任何视图控制器在应用中被推入,就像在问题中询问的一样? –

+0

@AuRis如果你投出成功,这意味着你的视图控制器只是推动,我认为这是足够清楚:/ –

+0

首先你的代码不会编译,因为你缺少一个括号。其次,如果您需要检查每个可能的课程,我不会称之为解决方法。最重要的是,如果演员成功并不意味着视图控制器被推动。 –

0

嗯,我有一个hacky的方式,因为你不想在你的viewcontrollers中写任何代码。看看我的类别下面为你

的UINavigationController + PushNotifier.h

#import <UIKit/UIKit.h> 

@interface UINavigationController (PushNotifier) 
extern void PJSwizzleMethod(Class cls, SEL originalSelector, SEL swizzledSelector); 

@end 

的UINavigationController + PushNotifier.m

#import "UINavigationController+PushNotifier.h" 
#import <objc/runtime.h> 


@implementation UINavigationController (PushNotifier) 

+ (void)load { 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     PJSwizzleMethod([self class], 
         @selector(pushViewController:animated:), 
         @selector(pj_pushViewController:animated:)); 
    }); 
} 

- (void)pj_pushViewController:(UIViewController *)viewController animated:(BOOL)animated { 
    //notify your Application Delegate 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ViewControllerWillPush" object:nil userInfo:@{@"pushingViewController":viewController}]; 
    return [self pj_pushViewController:viewController animated:animated]; 
} 

@end 

void PJSwizzleMethod(Class cls, SEL originalSelector, SEL swizzledSelector) 
{ 
    Method originalMethod = class_getInstanceMethod(cls, originalSelector); 
    Method swizzledMethod = class_getInstanceMethod(cls, swizzledSelector); 

    BOOL didAddMethod = 
    class_addMethod(cls, 
        originalSelector, 
        method_getImplementation(swizzledMethod), 
        method_getTypeEncoding(swizzledMethod)); 

    if (didAddMethod) { 
     class_replaceMethod(cls, 
          swizzledSelector, 
          method_getImplementation(originalMethod), 
          method_getTypeEncoding(originalMethod)); 
    } else { 
     method_exchangeImplementations(originalMethod, swizzledMethod); 
    } 
} 

注:如果你使用这只会工作导航控制器和推/视图控制器

相关问题