2
votes

I am using a side view controller setup using this : https://github.com/edgecase/ECSlidingViewController

The initial View Controller is loaded using the storyboard with an identifier of 'InitialViewController'.

Once loaded in the viewDidLoad for this, I check if the user is logged in using the below:

UIStoryboard *storyboard;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
}

if (![PFUser currentUser]){
    self.topViewController = [storyboard instantiateViewControllerWithIdentifier:@"WelcomeVC"];
} else {
    self.topViewController = [storyboard instantiateViewControllerWithIdentifier:@"HomeVC"];
}

Per the above, if the user is not logged in it loads the WelcomeVC. The WelcomeVC is a Navigation Controller that has 3 Vc's within it. Welcome/Login/Signup.

Once the user logs in I need to change the topViewController (like above) to be the HomeVC. The navigation controller known as WelcomeVC can be dismissed, if possible. How do I change this topViewController I have tried this but it does not work:

UIStoryboard *storyboard;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
}

self.EVC = [storyboard instantiateViewControllerWithIdentifier:@"InitialViewController"];
self.EVC.topViewController = [storyboard instantiateViewControllerWithIdentifier:@"HomeVC"];
2

2 Answers

1
votes

Personally, I like to make the home screen to be the root view controller from the start, and have viewDidAppear automatically take me to the login screen via a non-animated modal segue (I prefer performSegueWithIdentifier so that the storyboard's segues show the whole flow, rather than manually instantiating the controller and calling pushViewController, but whatever works for you). I then, upon successful login, simply dismiss back to the main screen. That way, I don't have to play around with changing the top level controller.

So my storyboard looks like:

enter image description here

So my main "home" view controller has the following viewDidAppear:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if (![[Model sharedManager] loggedIn])
        [self performSegueWithIdentifier:@"login" sender:self];
}

Clearly, your logic for determining whether you're logged in will vary, but you get the idea. My login does a modal segue (sans animation) to the login screen and I happen to be using a navigation controller on the series of login screens.

My final, successful login process then does:

- (IBAction)didTouchUpInsideDoneButton:(id)sender
{
    // ok, assuming at this point that everything has been validated and 
    // I'm ready to return, so just dismiss the navigation controller.
    // You could use unwind segue in iOS 6, as well.

    [[Model sharedManager] setLoggedIn:YES];

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
0
votes

In application:didFinishLaunchingWithOptions:, I check to see if a user is logged in already, and if they are, I jump to the profile:

-(void)goToProfile {
    UIStoryboard *storyboard = self.window.rootViewController.storyboard;
    UINavigationController *rootNav = [storyboard instantiateViewControllerWithIdentifier:@"MainNavigationController"];
    if (![self.window.rootViewController isKindOfClass:[rootNav class]]) {
        self.window.rootViewController = rootNav;
    }
    else {
        [(UINavigationController *)self.window.rootViewController popToRootViewControllerAnimated:YES];
    }
}

The didFinishLaunchingWithOptions doesn't need anything else in it.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    if ([self isLoggedIn]) {
        [self goToProfile];
    }
    else {
        NSLog(@"Not logged in");
    }
    return YES;    
}

In my storyboard, the primary branch goes to a signup process, and the navigation controller of the logged in view has a storyboard ID of MainNavigationController. my storyboard