1

self.window.rootViewController.presentedViewController

尽管有 viewController 可用,但总是返回nil 。不知道我做错了什么。

以下是完整代码-

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

3 回答 3

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
                }

          }
于 2016-06-28T10:12:51.320 回答
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];
于 2016-06-28T10:12:52.080 回答
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

    }

}
于 2016-06-28T11:25:57.260 回答