YasBES
YasBES

Reputation: 2325

how to change the rootviewcontroller

I want to change the rootViewController after the authenticationViewController enter image description here

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

this code is when i press in login button i want CondidatsViewController or RecruteursViewController will be the 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;
}

Upvotes: 2

Views: 5023

Answers (2)

Abduliam Rehmanius
Abduliam Rehmanius

Reputation: 928

You can try UINavigationController's following method with a new array of desired view controllers something like

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

It will do the trick ;)

Upvotes: 3

JeffRegan
JeffRegan

Reputation: 1322

How about have a view controller one level above your nav and auth view controllers? This view controller can check for a valid session and push whatever view is appropriate on top of the stack. It seems ill advised to try to change your root view controller.

Edit: More details

You have a root view controller that doesn't have a view tied to it like most other view controllers might. You set this class as the root view controller in your app delegate.

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

In your root view controller you can determine if you have a valid session or not. Based on that, you can render the appropriate view.

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

This is pretty rough, but it should point you in the right direction.

Upvotes: 1

Related Questions