这是我的 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
}
答案 0 :(得分: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
名称。
答案 1 :(得分:1)
轻松解决我的问题。
将这两种方法放在 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&#39> 方法,无论您是否需要检查:
ViewController *viewController = [[ViewController alloc] initWithNibName:[CommonUtils loadXIBBasedOnDevice:@"ViewController"] bundle:nil];
ViewController.navigationController.navigationBarHidden=NO;
[self.navigationController pushViewController:viewController animated:YES];
答案 2 :(得分:0)
也可以在不检查设备的if条件的情况下加载xib文件。
Apple已经提供此功能。你必须以这种格式给出xib文件名。
playerScreenViewController~iphone.xib // iPhone
playerScreenViewController~ipad.xib // iPad
此语句会根据您的设备自动获取正确的xib:
playerScreenViewController *playerScreen=[[playerScreenViewController alloc] initWithNibName:@"playerScreenViewController" bundle:nil];