2013-07-01 57 views
12

這是AppDelegate的didFinishLaunchingWithOptions方法。 讓我解釋一下情景,我已經在我的應用中開發了類似facebook的sideMenu,但現在我必須根據屏幕更改sideMenu列表(ViewController)如何從其他ViewController中更改AppDelegate中的RootViewController?

這裏的Side Menu是SideMenuViewController,它是contains中的一個參數,最終成爲窗口的rootViewController。

左右,非常基本的問題出現在「如何更改成爲窗口RootViewController的控制器或變量」如果任何程序員想知道更多的代碼或要求

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

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

SideMenuViewController *leftMenuViewController = [[SideMenuViewController alloc] init]; 

self.container = [ContainerOfSideMenuByVeerViewController 
        containerWithCenterViewController:[self navigationController] 
        leftMenuViewController:leftMenuViewController]; 

self.window.rootViewController = self.container; 

[self.window makeKeyAndVisible]; 

return YES; 

} 

,我歡迎通過提供編輯我的代碼或評論。

回答

13

試試這個:

<YourAppDelegateClass> *app = [[UIApplication sharedApplication] delegate]; 
app.window.rootViewController = <YourRootViewController>; 

不要忘了,包括必要的標頭(AppDelegate中,例如),或者你會得到CE。 這一個似乎工作:enter image description here

+0

親愛的,我已經試過了,它給我帶來的主要的Windows屏幕,但是親愛的我已經不是主控制器推導航兩個控制器裏面,這樣的嗎? –

+0

好吧,您可以使用' .navigationController.viewControllers'訪問導航控制器中的控制器堆棧。它返回NSArray。並嘗試用您的新陣列替換此陣列中的第一個控制器。 – Renderhp

+0

不完全,但你的方式幫助我很多解決我的問題。由於@renderhp –

13

隨着故事板

內的另一個的ViewController您可以使用此代碼:

self.view.window.rootViewController = [self.view.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"**STORYBOARD ID**"]; 

的AppDelegate

self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"**STORYBOARD ID**"]; 
8

知識共享使用斯威夫特:

從比應用delegate.swift其他類更改根視圖控制器

let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController 
let nav = UINavigationController(rootViewController: homeViewController) 
appdelegate.window!.rootViewController = nav 

更改RootViewController的動畫可實現與:

UIView.transitionWithView(self.window!, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromLeft, animations: { 
     self.window?.rootViewController = anyViewController 
}, completion: nil) 

我們可以寫泛化方法與this太相似。

希望這能有人幫助。

相關問題