Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在运行时更改方向时更改ViewController_Ios_Objective C_Ipad_Cocoa Touch - Fatal编程技术网

Ios 在运行时更改方向时更改ViewController

Ios 在运行时更改方向时更改ViewController,ios,objective-c,ipad,cocoa-touch,Ios,Objective C,Ipad,Cocoa Touch,我想更改屏幕上的ViewController方向更改 我只有一个类ViewController和两个基于故事板的UIViewController UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideD

我想更改屏幕上的ViewController方向更改

我只有一个类ViewController和两个基于故事板的UIViewController

  UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)
    {
        [[NSBundle mainBundle] loadNibNamed:@"Portrait" owner:self options:nil];
    }
    else {
        [[NSBundle mainBundle] loadNibNamed:@"Landscape" owner:self options:nil];
    }

该代码可用于在方向改变时加载不同的xib。但是,在方向更改时,如何在Xib文件中加载不同的控制器?

如果采用这种方法,则不会在Xib中加载不同的视图控制器。一个xib表示一个视图及其子视图

然而,我会遵循另一种方法。我将创建一个视图a(“主机”视图控制器的self.view),它只有两个全屏容器视图,一个用于横向视图,一个用于纵向视图。 只需将两者都自动调整大小,以便它们始终占据整个屏幕

在旋转的伊夫茨,我只能相应地隐藏和解开它们


但为什么您认为每次旋转都需要单独的视图控制器

这里最好的方法可能是使用。您应该做的是使用一个包装器视图控制器,它嵌入来自两个视图控制器的两个视图(每个方向一个),并使用iOS API提供的方法在视图之间切换

   - (void)viewDidLoad
     {
          [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)  name:UIDeviceOrientationDidChangeNotification  object:nil];   
     }

     -(void)orientationChanged:(NSNotification *)notification
      {
           UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

           if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
           {
                [[NSBundle mainBundle] loadNibNamed:@"Portrait" owner:self options:nil];            
           }
           else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
           {
                [[NSBundle mainBundle] loadNibNamed:@"Landscape" owner:self options:nil];
           }
      }

希望这能帮助你……

动画怎么样,看起来会不会很奇怪?你可以为隐藏和取消隐藏设置动画。你试过了吗?