【问题标题】:how to check device is ipad or iphone in appdelegate for xib's?如何在 xib 的 appdelegate 中检查设备是 ipad 还是 iphone?
【发布时间】:2014-10-16 07:36:45
【问题描述】:

这是我的 didFinishLaunchingWithOptions
目前我的应用程序仅适用于 iPhone,但我想要这两者(通用)。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    playerScreenViewController *playerScreen=[[playerScreenViewController alloc] initWithNibName:@"playerScreenViewController" bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:playerScreen];
    self.window.rootViewController = navigationController;
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    self.window.backgroundColor = [UIColor grayColor];
    [self.window makeKeyAndVisible];
    return YES;
}

而且我不知道如何在 appdelegate 中使用这个条件:

if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    // iPad
} else {
    // iPhone
}

【问题讨论】:

    标签: ios objective-c xib ios-universal-app


    【解决方案1】:

    正如我在代码中看到的,您将Xib 用于playerScreenViewController。现在您需要为 iPad 环境创建Xib

    然后把你的代码写成这样:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
            playerScreenViewController *playerScreen;
            if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
                // iPad
                playerScreen=[[playerScreenViewController alloc] initWithNibName:@"playerScreenViewController_iPad" bundle:nil];
    
            } else {
                // iPhone
                playerScreen=[[playerScreenViewController alloc] initWithNibName:@"playerScreenViewController" bundle:nil];
    
            }
    
            UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:playerScreen];
            self.window.rootViewController = navigationController;
            [[UIApplication sharedApplication] setStatusBarHidden:YES];
            self.window.backgroundColor = [UIColor grayColor];
            [self.window makeKeyAndVisible];
            return YES;
        }
    

    注意这一行

    playerScreen=[[playerScreenViewController alloc] initWithNibName:@"playerScreenViewController_iPad" bundle:nil];
    

    这里你必须为 iPad 环境传递 Xib 名称。

    【讨论】:

      【解决方案2】:

      也可以在不检查设备的 if 条件的情况下加载 xib 文件。

      Apple 已提供此功能。您只需提供这种格式的 xib 文件名。

      playerScreenViewController~iphone.xib // iPhone
      playerScreenViewController~ipad.xib // iPad
      

      此语句会根据您的设备自动采用正确的 xib:

          playerScreenViewController *playerScreen=[[playerScreenViewController alloc] initWithNibName:@"playerScreenViewController" bundle:nil];
      

      【讨论】:

        【解决方案3】:

        我的问题的简单解决方案。
        将这两个方法放在Helper Class中,这里我使用“CommonUtils”。

        +(BOOL)isiPad
        {
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            {
                return YES;
            }
            else
            {
                return NO;
            }
        }
        
        +(NSString *)loadXIBBasedOnDevice:(NSString *)viewControllerName
        {
            NSString *strReturn = @"";
            if([CommonUtils isiPad])
            {
                strReturn = [NSString stringWithFormat:@"%@-iPad",viewControllerName];
            }
            else
            {
                strReturn = viewControllerName;
            }
            NSLog(@"%@",strReturn);
            return strReturn;
        }  
        

        并把它放在 Viewcontroller 的方法上是否要检查。:

        ViewController *viewController = [[ViewController alloc] initWithNibName:[CommonUtils loadXIBBasedOnDevice:@"ViewController"] bundle:nil];
        ViewController.navigationController.navigationBarHidden=NO;
        [self.navigationController pushViewController:viewController animated:YES];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-05-19
          • 1970-01-01
          • 1970-01-01
          • 2012-09-10
          • 1970-01-01
          • 2012-11-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多