我很想知道我是否可以开发一个具有 2 个视图控制器的应用程序。我已经浏览了一些链接,但如果我使用故事板则找不到解决方案。如果我已经有一个 rooviewcontroller,我该如何删除它和添加另一个视图作为rootviewcontroller?有什么想法吗?
问问题
324 次
2 回答
3
你可以这样做。您只需将下面的代码添加到要更改 rootViewController 的位置/操作。
//First dismiss your currently loaded ViewController
[self dismissViewControllerAnimated:YES completion:nil];
//Get the keyWindow of the app
UIWindow *window = [[UIApplication sharedApplication]keyWindow];
NSString *identifier = @"Your_Identifier_Name_For_ViewController";// this is the identifier name(Storyboard ID)
// of the AnotherRootViewController
// which you have to set in your Storyboard
// as shown in the figure.
//Now create an object of the AnotherRootViewController
AnotherRootViewController *newRootViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
//Finally set your newRootViewController
[window setRootViewController:newRootViewController];
并将AnotherRootViewController设置为Storyboard,如图:
让我知道它是否满足您的要求。
于 2014-01-15T14:46:33.710 回答
1
应用中一次只有一个rootviewController,
您可以直接使用以下替换,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
YourVC *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"YourVC"];
self.window.rootViewController = rootViewController;
在 appDelegate 方法中,
于 2014-01-15T13:33:10.150 回答