thitemple
thitemple

Reputation: 6059

Changing the views from inside a subview

I have a root view controller that is loading a simple UIViewController which is responsible for authenticating the users.

After the user is authenticated I'd like my root view to change views and load a UINavigationController

My root controller is like this:

@class LoginViewController;
@class NavigationController;

@interface SwitchViewController : UIViewController {
    LoginViewController *loginViewController;
    NavigationController *navigationController;
}

@property (nonatomic, retain) NavigationController *navigationController;
@property (nonatomic, retain) LoginViewController *loginViewController;

- (IBAction) showDocuments;

@end

And the implementation of the showDocuments method is:

- (IBAction) showDocuments {
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    NavigationController *navController = [[NavigationController alloc] initWithNibName:@"NavigationControlller" bundle:nil];
    self.navigationController = navController;
    [navController release];

    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
    [navigationController viewWillAppear:YES];
    [loginViewController viewWillDisappear:YES];

    [loginViewController.view removeFromSuperview];
    [self.view insertSubview:navigationController.view atIndex:0];
    [loginViewController viewDidDisappear:YES];
    [navigationController viewDidAppear:YES];
}

On my login view I did this:

if ([self authenticate]) {
        SwitchViewController *switchController = (SwitchViewController *)self.parentViewController;
        [switchController showDocuments];
    }

But nothings happens. What am I missing?

Upvotes: 1

Views: 135

Answers (1)

Jonah
Jonah

Reputation: 17958

viewWillAppear and viewWillDisappear are meant to notify a controller that its view will be shown or hidden. Those method do nothing to actually present or hide views. If you are going to take over responsibility for managing view controller's view like this then you will need to add those views to the window yourself.

However I would discorage you from following this approach. UIViewController's view are expected to fill the window such that you only have a single view controller's view visible at once (with exceptions for Apple's container view controllers). By displaying one view controller's view within another view controller's view you will not see many of the behaviors you could otherwise expect from a UIViewController.

See http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/

Upvotes: 1

Related Questions