self.window.rootViewController.presentedViewController返回nil

时间:2016-06-28 09:53:23

标签: ios rootview rootviewcontroller

self.window.rootViewController.presentedViewController

总是返回 nil 虽然有viewController可用。 不确定我做错了什么。

以下是完整代码 -

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{

NSLog(@"this is loaded");
if ([self.window.rootViewController.presentedViewController isKindOfClass:[SecondViewController class]])
{
    SecondViewController *secondController = (SecondViewController *) self.window.rootViewController.presentedViewController;

    if (secondController.isPresented)
    {
        return UIInterfaceOrientationMaskLandscape;
    }
    else return UIInterfaceOrientationMaskPortrait;
}
else return UIInterfaceOrientationMaskPortrait;
}

3 个答案:

答案 0 :(得分:1)

self.window.rootViewController.presentedViewController。我认为它会返回UINavigationController类型类。请检查日志或调试。

 UINavigationController* navigationController = (UINavigationController*)self.window.rootViewController.presentedViewController;

NSArray *arrayVC =navigationController.viewControllers;
        for (UIViewController* viewController in arrayVC) {

                //This if condition checks whether the viewController's class is SecondViewController
                 if ([viewController isKindOfClass:[SecondViewController class]] ) 
                {
                    //Do something
                }

          }

答案 1 :(得分:1)

if([self.window.rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]) {
    UINavigationController* navigationController = (UINavigationController*)self.window.rootViewController.presentedViewController;
     if([navigationController.visibleViewController isKindOfClass:[SecondViewController class]])
     {
             SecondViewController *secondController = (SecondViewController *) self.window.rootViewController.presentedViewController;

             if (secondController.isPresented){
                  return UIInterfaceOrientationMaskLandscape;
             }
             else return UIInterfaceOrientationMaskPortrait;
     }
     else return UIInterfaceOrientationMaskPortrait;
}

已编辑:

[self.navigationController pushViewController:detailViewController animated:NO];

替换为:

[self.navigationController presentViewController:detailViewController animated:NO completion:nil];

答案 2 :(得分:0)

尝试使用此代码。在这里,您可以检查它是否已呈现或推送,还可以检查您的特定课程。

UIViewController *vcTmp = [[UIViewController alloc]init];

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

if([[appDelegate.window.rootViewController presentingViewController] presentedViewController])
{

    // Now your viewcontroller is presented

    vcTmp = [[appDelegate.window.rootViewController presentingViewController] presentedViewController];

    if ([vcTmp isKindOfClass:[MasterViewController class]]){

            // Your class is identified here

    }

}
else
{

    // Now your viewcontroller is pushed

    vcTmp = [[appDelegate.window.rootViewController presentingViewController] presentedViewController];

    NSArray *viewControllers = [appDelegate.window.rootViewController childViewControllers];

    vcTmp = (UIViewController*)viewControllers.lastObject;

    if ([vcTmp isKindOfClass:[MasterViewController class]]){

            // Your class is identified here

    }

}