2 mapmapm MapMapM 于 2012.12.10 13:28 提问

如何根据设备类型启动特定的视图

在启动时候有三个视图,想要实现的是可以根据设备类型来选择三个中的一个。目前我写的代码在AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // 应用启动后重写制定
    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];
    }
}

但是运行之后,应用启动的时候屏幕一片漆黑。
怎么解决,谢谢。

2个回答

lanhailvcao
lanhailvcao   2012.12.10 14:07
已采纳

在方法的最后添加:

self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
xiaoyan_12
xiaoyan_12   2012.12.10 13:43

用以下代码,更简洁一些。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    NSString *nibName;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        nibName = @"ViewController_PortraitPad";
    } else {
        if ([UIScreen mainScreen].bounds.size.height == 480.0) {
            nibName = @"ViewController_Portrait4";
        } else {
            nibName = @"ViewController_Portrait5";
        }
    }

    self.viewController = [[ViewController alloc] initWithNibName:nibName bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!