Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

rootviewcontroller - Changing root view controller of a iOS Window

Is the root view controller of a iOS Window usually initialized once in the beginning to a tab bar controller or navigation controller? Is it okay to change the root view controller multiple times within an app?

I have a scenario where the top view is different based on user action. I was thinking of having a navigation controller with the top view controller having the image of the splash screen, and pushing/popping view controllers as required. Alternately, I can keep changing the window's top view controller. Which will be a better approach?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

iOS 8.0, Xcode 6.0.1, ARC enabled

Most of your questions were answered. However, I can tackle one that I recently had to deal with myself.

Is it okay, to change the root view controller multiple times, within an app?

The answer is yes. I had to do this recently to reset my UIView hierarchy after the initial UIViews that were part of the app. starting up were no longer needed. In other words, you can reset your "rootViewController" from any other UIViewController at anytime after the app. "didFinishLoadingWithOptions".

To do this...

1) Declare a reference to your app. delegate (app called "Test")...

TestAppDelegate *testAppDelegate = (TestAppDelegate *)[UIApplication sharedApplication].delegate;

2) Pick a UIViewController you wish to make your "rootViewController"; either from storyboard or define programmatically...

    a) storyboard (make sure identifier, i.e. storyboardID, exists in Identity Inspector for the UIViewController):
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

NewRootViewController *newRootViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"NewRootViewController"];

    b) programmatically (could 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 throw in an 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! Cheers.

The root view controller for the window.

The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed. The default value of this property is nil.

*Update 9/2/2015

As comments below point out, you must handle the removal of the old view controller when the new view controller is presented. You may elect to have a transitional view controller in which you will handle this. Here are a few hints 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;

                }];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...