2012-01-03 39 views
19

我想唯一的應用程序轉換我的iPhone到普遍應用。我將設備切換到Universal,讓Xcode爲我製作MainWindow-iPad.xib,現在當我在iPhone模擬器中運行應用程序時,它可以正常工作,但是當我在iPad模擬器中運行它時,屏幕和Application windows are expected to have a root view controller at the end of application launch錯誤。我已閱讀了一些關於這個相同問題的其他文章,但沒有一篇僅限於一個設備。「應用程序窗口預計將有在應用程序啓動的最後一個根視圖控制器」錯誤只在iPad上

這裏是我的application:didFinishLaunchWithOptions:方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

/* some dropbox setup stuff */ 


// INIT VIEW AND CORE DATA 
RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 

NSManagedObjectContext *context = [self managedObjectContext]; 
if (!context) { 
    // Handle the error. 
} 

rootViewController.managedObjectContext = context; 

UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController]; 
self.navigationController = aNavigationController; 

[_window addSubview:[_navigationController view]]; 
[_window makeKeyAndVisible]; 

[rootViewController release]; 
[aNavigationController release]; 

return YES; 
} 

編輯:我只是有大小適合iPhone稱爲RootViewController的一個根視圖控制器。但它應該仍然是不應該加載?或者,如果它不應該如何爲iPad創建一個?

+2

愚弄:http://stackoverflow.com/q/7520971/9530 http://stackoverflow.com/q/12784411/9530 http://stackoverflow.com/q/8190567/9530的http://計算器。 COM/q /九千五百三十零分之一千一百五十一萬五千八百十八http://stackoverflow.com/q/9844626/9530甚至更多 – 2013-03-08 20:40:31

回答

52

更改以下行:

[_window addSubview:[_navigationController view]]; 

到:

_window.rootViewController = _navigationController; 

,或者,如果你需要的iOS 3兼容性:

if ([_window respondsToSelector:@selector(setRootViewController:)]) { 
    _window.rootViewController = _navigationController; 
} else { 
    [_window addSubview:_navigationController.view]; 
} 
+1

感謝,但這並沒有改變任何東西。任何其他建議? – 2012-01-03 01:16:07

+1

爲我工作,謝謝 – davis 2012-07-10 23:31:14

+1

優秀。也爲我工作。謝謝。 – 2012-09-26 04:30:33

2

您需要創建與廈門國際銀行一個RootViewController的iPad的文件,否則你會得到這個錯誤。以下是Xcode爲通用應用程序提供的模板代碼。如果您在iPad模擬器中調試應用程序並指向調試器運行,則使用iPhone xib文件創建視圖控制器,您將看到確切的錯誤。

- (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) { 
     self.viewController = [[SYKViewController alloc] initWithNibName:@"SYKViewController_iPhone" bundle:nil]; 
    } else { 
     self.viewController = [[SYKViewController alloc] initWithNibName:@"SYKViewController_iPad" bundle:nil]; 
    } 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 
2

在iOS中4和以後,UIWindow具有可設置屬性rootViewController。這是UINavigationController,它推動應用程序啓動時顯示的UIViewController。在Xcode IB中,選擇初始場景:對於UINavigationController,初始視圖控制器設置了所有內容,無需代碼。

0

我想你的建議,但他們沒有工作對我來說,對不起。 :/我最終只是手動使視圖中的代碼,而界面生成器:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
    // Setup view for iPad 
} else 
    // Setup view for iPhone and iPod Touch 
} 

我本以爲這是很多困難比實際的。

請注意,如果您使用此方法,你仍然可以掛接在界面生成器的一切,只是改變對象的框架在這​​些塊,如果你要對iPhone和iPad相同的對象。

2

從所生成的主窗口-iPad.xib,在界面生成器,加入IB視圖控制器對象,以及在視圖下方的Object。對於該對象,將其類設置爲AppDelegate,對於View Controller,將該類設置爲ViewController(即Identity Inspector),並在Attributes Inspector中指定nib名稱。您可以查看您正在轉換的設備的MainWindow.nib以查看差異。

編輯:我忘了提一些重要的步驟。您還需要在IB中將文件所有者類設置爲「UIApplication」,併爲App Delegate和View Controller適當地設置參考插口。同樣,在IB中,最簡單的方法是查看Connections Main Inspector中的MainWindow nib並對其進行仿真。如果您有另一個特定於iPad的筆尖,除了MainWindow-iPad.nib(即ViewController-iPad)。NIB),一定要選擇它的文件的所有者,並將其指向視圖,並設置它的類太合適。

相關問題