ios 如何从其他 ViewController 更改 AppDelegate 中的 RootViewController?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17408171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:10:25  来源:igfitidea点击:

How to change RootViewController in AppDelegate From Other ViewController?

iosappdelegaterootview

提问by Chatar Veer Suthar

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)

这就是 AppDelegate 中的 didFinishLaunchingWithOptions 方法。让我解释一下场景,我在我的应用程序中开发了像 facebook 这样的 sideMenu,但现在我必须根据屏幕(ViewController)更改 sideMenu 列表

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

这里的side Menu是SideMenuViewController,它是contain中的一个参数,最终成为window的rootViewController。

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

所以,出现的非常基本的问题是“如何更改成为 windows 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;

}

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

如果有程序员想了解更多代码或需求,欢迎通过编辑我的代码或在评论中提供。

采纳答案by Renderhp

Try this:

尝试这个:

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

Don't forget to include necessary headers (your AppDelegate, for example) or you'll get CE. This one seems to work:enter image description here

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

回答by Anton Gaenko

With Storyboard

故事板

Inside another ViewControlleryou can use this code:

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

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

Inside AppDelegate:

AppDelegate内部:

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

回答by Arvind

Knowledge Sharing Using Swift:

使用 Swift 进行知识共享:

Changing root view controller from class other than app delegate.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

Changing rootviewcontroller With Animation can be achieve with:

使用动画更改 rootviewcontroller 可以通过以下方式实现:

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

We can write generalise method too similar to this.

我们可以编写与过于相似的通用方法。

Hope this will helpful for someone.

希望这对某人有帮助。