iOS Objective-C:如何识别视图控制器正在通过UinavigationController呈现



我在实现上有多个ViewControllers:

ViewControllerA
ViewControllerB
ViewControllerC
ViewControllerD

但是,我需要将它们加载到ViewControllerc中,但我不知道该ViewController是否已加载(初始化(或是否存在。

我尝试了AppDeelegate的此操作:

ViewControllerC *rootViewController = [[ViewControllerC alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

,但似乎正在创建ViewController的新实例。

我向大家提出的问题,如何在应用程序中捕获Instace viewControllerc加载它,或者如何检测是否尚未加载viewControllerc?

我真的很感谢您的帮助或解决。

正如您指出的那样,分配视图控制器以确定是否呈现它是没有意义的。您的应用程序是否始终具有导航控制器的根源?如果是这样,您可以以这种方式获得...

// in the app delegate
AppDelegate *appDelegate = self;
// or, if not in the app delegate
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// either way
UINavigationController *navController = (UINavigationController *)[[appDelegate window] rootViewController];

请注意,Root VC的潜在鲁ck铸件作为UINavigationController。只有当有时可能是其他类型的VC时,这才是鲁ck的。如果这是您应用的情况,那么您需要测试...

UIViewController *vc = [[appDelegate window] rootViewController];
if ([vc isKindOfClass:[UINavigationController self]]) {
    UINavigationController *navController = (UINavigationController *)vc;
    // carry on from here
} else {
    // decide what your "deep link" function does when the wrong root vc is present.  maybe start over?
}

最后,我认为您遇到的问题,我们如何确定是否存在ViewControllerc,以及我们如何提出呢?第一部分很容易,因为导航控制器具有viewControllers属性。那是代表"堆栈"的数组,其中第一个项目是根,最后一项在顶部。所以...

NSInteger index = NSNotFound;
for (UIViewController *vc in navController.viewControllers) {
    if ([vc isKindOfClass:[UIViewController self]]) {
        index = [navController.viewControllers indexOfObject:vc];
    }
}
if (index != NSNotFound) {
    // it's on the stack
}

这是询问它是否在堆栈顶部的方法...

[navController.viewControllers.lastObject isKindOfClass:[ViewControllerC self]]

如果不在堆栈上该怎么办。一个想法是只推一个。这样做的方式已经在应用程序中。如果它在堆上,但不在顶部怎么办?如果您希望动画到达那里,您会弹出它(使最后一个流行音乐动画(。由于这是一个深厚的链接,因此您可能不在乎动画。只需截断NAV控制器视图控制器列表...

if (index != NSNotFound) {
    // it's on the stack
    navController.viewControllers = [navController.viewControllers subarrayWithRange:NSMakeRange(0, index+1)];
}

用于检查root View Controller是否是ViewControllerc

Swift:

if type(of: UIApplication.shared.keyWindow?.rootViewController) == ViewControllerC.self{
    debugPrint("RootViewController is a ViewControllerC")
}

Objective-C:

if ([[[[UIApplication sharedApplication] keyWindow] rootViewController] class] == [ViewControllerC class]){
        NSLog(@"RootViewController is a ViewControllerC");
    }

用于检查是否在root View Controller上显示ViewControllerc

Swift:

if let rootViewController = UIApplication.shared.keyWindow?.rootViewController{
    if type(of: rootViewController.presentedViewController) == ViewControllerC.self{
        debugPrint("ViewControllerC is presented on rootViewController")
    }
}

Objective-C:

UIViewController *viewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
    if (viewController != nil){
        if ([viewController.presentedViewController class] == [ViewControllerC class]){
            NSLog(@"ViewControllerC is presented on rootViewController");
        }
    }

将root View Controller设置为ViewControllerc

Swift:

if UIApplication.shared.keyWindow != nil{
    let viewController:ViewControllerC = ViewControllerC()
    //You can get above instance from Storyboard if you wanna
    UIApplication.shared.keyWindow!.rootViewController = viewController
}

Objective-C:

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
if (window != nil){
    ViewControllerC *viewController = [[ViewControllerC alloc] init];
    //You can get above instance from Storyboard if you wanna
    window.rootViewController = viewController;
}

对于ROOT的导航控制器上的推动视图控制器,如果存在

Swift:

    if UIApplication.shared.keyWindow != nil{
        if let navigationController = UIApplication.shared.keyWindow!.rootViewController as? UINavigationController{
        let viewController:ViewControllerC = ViewControllerC()
        //You can get above instance from Storyboard if you wanna
        navigationController.pushViewController(viewController, animated: true)
        }
    }

Objective-C:

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    if (window != nil){
        UINavigationController *navigationController = (UINavigationController*)window.rootViewController;
        if (navigationController != nil){
            ViewControllerC *viewController = [[ViewControllerC alloc] init];
            [navigationController pushViewController:viewController animated:true];
        }
    }

现在您可以做很多事情,例如,如果存在

,请从导航控制器中获取ViewControllerc的实例

Objective-C:

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    if (window != nil){
        UINavigationController *navigationController = (UINavigationController*)window.rootViewController;
        if (navigationController != nil){
            UIViewController *viewController = [navigationController topViewController];
            if ([viewController class] == [ViewControllerC class]){
                NSLog(@"Do what you want with viewControllerC instance");
            }
        }
    }

最新更新