self.window.rootViewController.presentedViewController retur



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;
}

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
                }
          }
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];

试试这段代码。这里你可以检查它是呈现的还是推送的,也可以检查你的特定类。

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
    }
}

最新更新