2012-12-08 18 views
0

我正試圖在啓動時啓動3個視圖中的1個。我想要啓動的視圖取決於設備類型。以下是我在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]; 
    } 
} 

到目前爲止的問題是,當我啓動應用程序,有一個黑色的屏幕。

+0

是什麼問題? – cscott530

回答

2

您沒有設置窗口中的控制器,在方法的末尾添加:

self.window.rootViewController = self.viewController; 
[self.window makeKeyAndVisible]; 
+0

由於未捕獲的異常'NSInternalInconsistencyException',我現在正在得到一個SIGABRT'終止應用程序,原因:' - [UIViewController _loadViewFromNibNamed:bundle:]加載了「ViewController_Portrait5」筆尖,但未設置視圖插座。'' –

+0

對不起,但請作爲一個單獨的問題發佈。這裏的目標是不要將一堆問題捆綁到一個帖子中... –

+0

http://stackoverflow.com/questions/13780016/sigabrt-error-view-outlet-was-not-set非常感謝你的連續幫幫我。我希望在所有這些之後,我會以某種方式回報你。 –

2

有編寫代碼(包括修復)更清潔的方式:

- (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]; 
相關問題