14

This is didFinishLaunchingWithOptions Method in AppDelegate. Let me explain scenario, I have developed sideMenu like facebook in my app, but now I have to change the sideMenu list according to screens (ViewController)

Here the side Menu is SideMenuViewController, which is an argument in contain, which ultimately becomes window's rootViewController.

SO, The very basic question arises is "How to change the controller or variable which becomes rootViewController of windows"

 - (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;

}

If any programmer wants to know more code or requirement, I do welcome to provide by editing my code or in comments.

4

3 回答 3

14

尝试这个:

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

不要忘记包含必要的标头(例如您的 AppDelegate),否则您将获得 CE。这个似乎有效:在此处输入图像描述

于 2013-07-01T15:32:29.270 回答
14

故事板

在另一个ViewController中,您可以使用以下代码:

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

内部AppDelegate

self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"**STORYBOARD ID**"];
于 2014-08-21T14:12:23.233 回答
11

使用 Swift 进行知识共享:

从 app 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)

我们可以编写与此类似的泛化方法。

希望这对某人有帮助。

于 2015-10-13T09:57:05.393 回答