从子视图内部更改视图

时间:2011-06-16 19:28:06

标签: objective-c ios

我有一个根视图控制器,它正在加载一个简单的UIViewController,它负责验证用户。

用户通过身份验证后,我希望我的根视图可以更改视图并加载UINavigationController

我的根控制器是这样的:

@class LoginViewController;
@class NavigationController;

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

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

- (IBAction) showDocuments;

@end

showDocuments方法的实现是:

- (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];
}

在我的登录视图中,我这样做了:

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

但是没有发生。我错过了什么?

1 个答案:

答案 0 :(得分:1)

viewWillAppearviewWillDisappear旨在通知控制器其视图将被显示或隐藏。这些方法无法实际呈现或隐藏视图。如果你要接管像这样管理视图控制器视图的责任,那么你需要自己将这些视图添加到窗口。

但是,我会劝你不要采用这种方法。 UIViewController的视图应填充窗口,以便您只能同时看到一个视图控制器的视图(Apple的容器视图控制器除外)。通过在另一个视图控制器的视图中显示一个视图控制器的视图,您将看不到许多您可能期望从UIViewController获得的行为。

请参阅http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/