2012-09-03 64 views
6

我新的iPhone,變化RootViewController的到導航控制器

我想改變我的Rootviewcontroller我的新類,並使其導航控制器。

這裏是我的代碼片段,

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


    UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:detailViewController]; 
    [self.window addSubview:navigationController.view]; 

    [self.window makeKeyAndVisible]; 


    return YES; 
} 

我得到SIGABRT說:'adding a root view controller <NewClass: 0x6a8dd50> as a child of view controller:

回答

12

每當ü要設置:

DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; 
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:detailViewController]; 
self.window.rootViewController =nil; 
self.window.rootViewController = navigationController; 
[self.window makeKeyAndVisible]; 

編輯:直接使用AppDelegateinstance設置rootViewControllerUIWindow如我上面顯示。

+0

同樣存在問題米發生,即使在寫這個.. – Krunal

+0

檢查編輯答案 –

+0

謝謝你的答案保存我的最後4小時的工作 –

2

相反的:

[self.window addSubview:navigationController.view]; 

使navigationControllerrootViewControllerwindow的:

self.window.rootViewController = navigationController; 

另外,是detailViewController類型UINavigationController?您不能將UINavigationController作爲根設置爲另一個UINavigationController對象。

+0

同樣的問題出現,即使寫這個了。 – Krunal

1

只需添加這條線,

RootViewController *defaultViewController=[[RootViewController alloc]initWithNibName:@"NAME_OF_XIB" bundle:nil];

之前的UINavigationController的初始化,

RootViewController *defaultViewController=[[RootViewController alloc]initWithNibName:@"NAME_OF_XIB" bundle:nil]; 
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:detailViewController]; 
[self.window addSubview:navigationController.view]; 

[self.window makeKeyAndVisible]; 
return YES; 
+0

讓我知道,如果這樣的作品 – channi

0

更改RootViewController的到NavigationController ..

UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:self.detailViewController]; 
相關問題