【问题标题】:How to switch views on orientation change如何在方向更改时切换视图
【发布时间】:2012-12-07 23:54:56
【问题描述】:

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

  • iPhone4_(320x480) 和肖像
  • iPhone4_(320x480) 和横向
  • iPhone5_(320x568) 和肖像
  • iPhone5_(320x568) 和横向
  • iPad_(768x1024) 和肖像
  • iPad_(768x1024) 和横向

我知道经历所有这些麻烦似乎有点愚蠢,但在这种情况下这是必需的。无论如何,我一直在尝试将来自Programming Guide 的 Apple 解决方案与来自TheAppCodeBlog 的在线教程中给出的方法结合起来。

ViewController.m --> ViewDidLoad 方法

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

ViewController.m --> orientationChanged 方法

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

}

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

我还收到Expected identifier 的一些错误:

更新

每当我启动应用程序时,都会出现黑屏。这是我的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];
    }
}

【问题讨论】:

    标签: iphone ios ipad optimization screen-orientation


    【解决方案1】:

    您与Expected Identifier 相关的错误是因为您的括号错误。您的if 条件应该是(我在其他几个条件中也看到相同的错误):

    else if (((deviceOrientation == UIInterfaceOrientationLandscapeLeft) || (deviceOrientation == UIInterfaceOrientationLandscapeRight)) && [UIScreen mainScreen].bounds.size.height == 480.0)
    

    【讨论】:

    • 非常感谢,现在我只是因为没有注意到这一点而感到愚蠢。在我添加到原始帖子的最后一次更新中,您有什么机会可以帮助我吗?
    • 我看到了问题并且知道您更新的解决方案。请把它作为一个单独的问题发布,我会在那里回答。同时,也请接受第一个问题的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 2020-12-10
    • 2022-01-10
    相关资源
    最近更新 更多