iOS 8.0, Xcode 6.0.1, ARC enabled
Most of your questions have been answered. However, I can solve the problem that I recently had to deal with.
Is it possible to change the root view controller several times in an application?
The answer is yes . I had to do this recently in resetting my UIView hierarchy after the initial UIView that were part of the application. launch is no longer needed. In other words, you can reset your "rootViewController" from any other UIViewController anytime after the application. "didFinishLoadingWithOptions".
To do this...
1) Declare a link to your application. delegate (application called "Test") ...
TestAppDelegate *testAppDelegate = (TestAppDelegate *)[UIApplication sharedApplication].delegate;
2) Select the UIViewController that you want to make your "rootViewController"; either from the storyboard, or determined programmatically ...
- a) storyboard (make sure the identifier, i.e. the storyboardID, exists in the Identity Inspector for the UIViewController):
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; NewRootViewController *newRootViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"NewRootViewController"];
- b) programmatically (possibly addSubview, etc.)
UIViewController *newRootViewController = [[UIViewController alloc] init]; newRootViewController.view = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 430)]; newRootViewController.view.backgroundColor = [UIColor whiteColor];
3) Putting it all together ...
testAppDelegate.window.rootViewController = newRootViewController; [testAppDelegate.window makeKeyAndVisible];
4) You can even add animation ...
testAppDelegate.window.rootViewController = newRootViewController; [testAppDelegate.window makeKeyAndVisible]; newRootViewController.view.alpha = 0.0; [UIView animateWithDuration:2.0 animations:^{ newRootViewController.view.alpha = 1.0; }];
Hope this helps someone! Greetings.
The root controller for the window.
The root view controller provides a view of the contents of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) sets the view controllers to view as window contents. If the window has an existing hierarchy view, the old views are deleted before the new ones are installed. The default value of this property is nil.
* Update 9/2/2015
As the comments below indicate, you should handle the removal of the old view controller when the new view controller is presented. You can choose a transitional view controller in which you will deal with this. Here are some tips on how to implement this:
[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; }];