Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.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
Iphone 如何在方向更改时切换视图_Iphone_Ios_Ipad_Optimization_Screen Orientation - Fatal编程技术网

Iphone 如何在方向更改时切换视图

Iphone 如何在方向更改时切换视图,iphone,ios,ipad,optimization,screen-orientation,Iphone,Ios,Ipad,Optimization,Screen Orientation,我目前正在创建一个包含六个视图的应用程序,每个视图都专用于设备类型和方向的组合。如果您不明白我的意思,我将标记所有六种组合: iPhone4(320x480)和肖像 iPhone4(320x480)和景观 iPhone5(320x568)和肖像 iPhone5(320x568)和景观 iPad(768x1024)和肖像 iPad(768x1024)和横向 我知道经历所有这些麻烦似乎有点傻,但在这种情况下这是必要的。不管怎样,我一直在尝试将苹果的解决方案与在线教程中给出的方法结合起来 View

我目前正在创建一个包含六个视图的应用程序,每个视图都专用于设备类型和方向的组合。如果您不明白我的意思,我将标记所有六种组合:

  • iPhone4(320x480)和肖像
  • iPhone4(320x480)和景观
  • iPhone5(320x568)和肖像
  • iPhone5(320x568)和景观
  • iPad(768x1024)和肖像
  • iPad(768x1024)和横向
我知道经历所有这些麻烦似乎有点傻,但在这种情况下这是必要的。不管怎样,我一直在尝试将苹果的解决方案与在线教程中给出的方法结合起来

ViewController.m
-->
ViewDidLoad
方法

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
- (void) orientationChanged:(NSNotification *)object
{
    UIDeviceOrientation deviceOrientation = [[object object] orientation];
    if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 480.0)
    {
        self.view = self.portrait4View;
    }
    else if ((deviceOrientation == UIInterfaceOrientationLandscapeLeft) || (deviceOrientation == UIInterfaceOrientationLandscapeRight)) && [UIScreen mainScreen].bounds.size.height == 480.0)
    {
        self.view = self.landscape4View;
    }
    else if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 568.0)
    {
        self.view = self.portrait5View;
    }
    else if ((deviceOrientation == UIInterfaceOrientationLandscapeLeft) || (deviceOrientation == UIInterfaceOrientationLandscapeRight)) && [UIScreen mainScreen].bounds.size.height == 568.0)
    {
        self.view = self.landscape5View;
    }
    else if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 1024.0)
    {
        self.view = self.portraitPadView;
    }
    else
    {
        self.view = self.landscapePadView;
    }

}
ViewController.m
-->
orientationChanged
方法

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
- (void) orientationChanged:(NSNotification *)object
{
    UIDeviceOrientation deviceOrientation = [[object object] orientation];
    if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 480.0)
    {
        self.view = self.portrait4View;
    }
    else if ((deviceOrientation == UIInterfaceOrientationLandscapeLeft) || (deviceOrientation == UIInterfaceOrientationLandscapeRight)) && [UIScreen mainScreen].bounds.size.height == 480.0)
    {
        self.view = self.landscape4View;
    }
    else if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 568.0)
    {
        self.view = self.portrait5View;
    }
    else if ((deviceOrientation == UIInterfaceOrientationLandscapeLeft) || (deviceOrientation == UIInterfaceOrientationLandscapeRight)) && [UIScreen mainScreen].bounds.size.height == 568.0)
    {
        self.view = self.landscape5View;
    }
    else if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 1024.0)
    {
        self.view = self.portraitPadView;
    }
    else
    {
        self.view = self.landscapePadView;
    }

}
每个视图的界面生成器设置:

我还收到了一条关于
预期标识符的错误消息

更新 每当我启动应用程序时,我都会看到一个黑屏。下面是我的
AppDelegate.m
的外观:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_Portrait5" bundle:nil];
    }
    
    else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 480.0) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_Portrait4" bundle:nil];
    }

    else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_PortraitPad" bundle:nil];
    }
}

预期标识符相关的错误是因为您的参数错误。您的
if
条件应该是(我在其他几个条件中也看到了相同的错误):


非常感谢你,现在我只是觉得自己很愚蠢,因为我没有注意到这一点。你有没有可能在我上次更新时也帮我添加到原始帖子中?我看到了问题,知道你更新的解决方案。请把它作为一个单独的问题贴出来,我会在那里回答。同时,请接受第一个问题的答案。谢谢