iOS 如何从应用程序中的任何位置更改根视图

How to Change Root View from anywhere in appliction in iOS

我开发了一个没有故事板的应用程序。

我有注销功能。当用户单击注销时,我必须将他重定向到登录页面并清除所有导航堆栈并创建登录页面根视图。

我正在尝试使用此代码,但使用此代码我的应用程序崩溃了:

 SignUpVC *main = [[SignUpVC alloc]initWithNibName:@"SignUpVC" bundle:nil];

UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:main];

[self presentViewController:main animated:NO completion:nil];

要更改应用程序的根视图,您可以通过这种方式进行

SignUpVC *main = [[SignUpVC alloc]initWithNibName:@"SignUpVC" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:main];
[[UIApplication sharedApplication].keyWindow setRootViewController:navController];