2017-10-13 3 views
0

AppDelegate에서 AppDelegate 및 UIViewController의 추가 메서드를 호출하지 않고 새 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에 대한 캐스팅을해야 할 것입니다 , 일부 사용자 정의 코드를 수행하는 것이 더 깨끗하고 사용자의 요구에 더 적합합니다.

+0

질문에서와 같이 앱에 뷰 컨트롤러가 푸시 된 경우 솔루션에서 어떻게 감지합니까? –

+0

@AuRis 당신이 성공했다는 것은 당신의 뷰 컨트롤러가 방금 눌려 졌다는 것을 의미한다. 나는 이것이 충분히 명확하다고 생각했다 :/ –

+0

우선, 하나의 중괄호가 없기 때문에 우선 코드가 컴파일되지 않을 것이다. 둘째, 각 가능한 클래스를 확인해야하는 경우 해결 방법이라고하지는 않습니다. 그리고 가장 중요한 것은 캐스트가 성공한다고해서 뷰 컨트롤러가 푸시되었음을 의미하지는 않습니다. –

0

글쎄, 난 당신을 위해 당신의 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); 
    } 
} 

참고 : 당신이 사용하는 경우이에만 작동합니다 네비게이션 컨트롤러 및 푸시/뷰 뷰 컨트롤러

관련 문제