如何更改rootviewcontroller

时间:2013-07-31 01:17:13

标签: iphone ios ios5

我想在authenticationViewController enter image description here

之后更改rootViewController
-(IBAction)LoginButtonPushed:(id)sender {
    if ([(VerifyId)isEqual:@"C"]){
        CondidatsViewController *condidatsViewController = [[[CondidatsViewController alloc]initWithNibName:@"CondidatsViewController" bundle:nil]autorelease];
        UINavigationController *navController = self.navigationController;

        NSMutableArray *controllers = [[self.navigationController.viewControllers mutableCopy] autorelease];
        [controllers removeLastObject];
        navController.viewControllers = controllers;
        [navController pushViewController:condidatsViewController animated: YES];

    } else {
        RecruteursViewController *recruteursViewController = [[[RecruteursViewController alloc]initWithNibName:@"RecruteursViewController" bundle:nil]autorelease];
        UINavigationController *navController = self.navigationController;

        NSMutableArray *controllers = [[self.navigationController.viewControllers mutableCopy] autorelease];
        [controllers removeLastObject];
        navController.viewControllers = controllers;
        [navController pushViewController:recruteursViewController animated: YES];
    }
}

此代码是当我按下登录按钮时我想要CondidatsViewController或RecruteursViewController将是rootView

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    AcceuilViewController *viewController =[[[AcceuilViewController alloc]
                                             initWithNibName:@"AcceuilViewController" bundle:nil]autorelease];
    self.navController = [[[UINavigationController alloc]initWithRootViewController:viewController]
                         autorelease];
   self.window.rootViewController = self.navController;
    [self.window makeKeyAndVisible];
    return YES;
}

2 个答案:

答案 0 :(得分:3)

您可以使用新的所需视图控制器数组尝试UINavigationController的以下方法,例如

    [self.navigationController setViewControllers:@[newRootViewControllerInstance, secondVCInstanceIfRequired, thirdVC-and-so-on....] animated:NO];

它会起作用;)

答案 1 :(得分:1)

如果将视图控制器放在导航和身份验证视图控制器上一层?此视图控制器可以检查有效会话并在堆栈顶部推送任何适当的视图。尝试更改根视图控制器似乎不明智。

编辑:更多详情

你有一个根视图控制器没有像大多数其他视图控制器那样绑定它的视图。您将此类设置为应用程序委托中的根视图控制器。

App Delegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    RootViewController *rootViewController = [[RootViewController alloc] init];

    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];

    return YES;
}

在根视图控制器中,您可以确定是否有有效会话。基于此,您可以渲染适当的视图。

RootViewController.m

@interface RootViewController ()
@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    BOOL isUserAuthenticated = [MyClasskToFigureOutIfUserIsAuthenticated isUserAuthenticated];

    if(isUserAuthenticated == NO) {
        AuthViewController *vcAuth = [[AuthViewController alloc] init];
        [self addChildViewController:vcAuth];
        [self.view addSubView:vcAuth.view];
    } else {
        //they are authenticated so push your other view controller.
    }
}

@end

这非常粗糙,但它应该指向正确的方向。