iOS 8.0、Xcode 6.0.1、ARC 已启用
您的大部分问题都已得到解答。不过,我可以解决我最近不得不自己处理的一个问题。
可以在应用内多次更改根视图控制器吗?
答案是是的。我最近必须这样做才能在作为应用程序一部分的初始 UIView 之后重置我的 UIView 层次结构。不再需要启动。换句话说,您可以在应用程序之后的任何时间从任何其他 UIViewController 重置您的“rootViewController”。 “didFinishLoadingWithOptions”。
为此...
1) 声明对您的应用的引用。委托(名为“Test”的应用)...
TestAppDelegate *testAppDelegate = (TestAppDelegate *)[UIApplication sharedApplication].delegate;
2) 选择一个你想要的 UIViewController 作为你的“rootViewController”;从情节提要或以编程方式定义...
a) 故事板(确保标识符,即storyboardID,存在
在 UIViewController 的身份检查器中):
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
NewRootViewController *newRootViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"NewRootViewController"];
b) 以编程方式(可以添加Subview 等)
UIViewController *newRootViewController = [[UIViewController alloc] init];
newRootViewController.view = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 430)];
newRootViewController.view.backgroundColor = [UIColor whiteColor];
3) 将它们放在一起...
testAppDelegate.window.rootViewController = newRootViewController;
[testAppDelegate.window makeKeyAndVisible];
4) 你甚至可以加入动画...
testAppDelegate.window.rootViewController = newRootViewController;
[testAppDelegate.window makeKeyAndVisible];
newRootViewController.view.alpha = 0.0;
[UIView animateWithDuration:2.0 animations:^{
newRootViewController.view.alpha = 1.0;
}];
希望这对某人有所帮助!干杯。
窗口的根视图控制器。
根视图控制器提供窗口的内容视图。
将视图控制器分配给此属性(以编程方式
或使用 Interface Builder)将视图控制器的视图安装为
窗口的内容视图。如果窗口具有现有视图
层次结构,旧视图在新视图被删除之前被删除
安装。该属性的默认值为 nil。
*2015 年 9 月 2 日更新
正如下面的 cmets 所指出的,当新的视图控制器出现时,您必须处理旧视图控制器的移除。你可以选择有一个过渡视图控制器来处理这个问题。以下是有关如何实现此功能的一些提示:
[UIView transitionWithView:self.containerView
duration:0.50
options:options
animations:^{
//Transition of the two views
[self.viewController.view removeFromSuperview];
[self.containerView addSubview:aViewController.view];
}
completion:^(BOOL finished){
//At completion set the new view controller.
self.viewController = aViewController;
}];