-2

I have this storyboard:

storyboard

The second viewcontroller is the login screen.

I check if user is already logged and, if its true, i want to skip the login screen and go to the tableviewcontroller. And, in the other hand, when user is not logged, show the login view and, when the user does the login, i want the next view to be the first in the navigation stack.

Now I'm setting the tableview as root element with this code explained in other post

NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
InitialViewController *nvc = [[InitialViewController alloc] init];
[viewControllers replaceObjectAtIndex:0 withObject:nvc];
[self.navigationController setViewControllers:viewControllers];

It works, but when i want to click in a cell that makes a segue to detail view it doesn't work. if i delete the login screen and sets the table view as root in navigation controller it works.

Anyone knows why?

Other ways to skip login view are appreciated

EDIT: With this solution it works, now i have two navigation controllers and i change from one to another with this code

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
InitialViewController *initial = [storyBoard instantiateViewControllerWithIdentifier:@"appRootController"];
AppDelegate *myAppDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
myAppDelegate.window.rootViewController = initial;

I don't know if it's the right solution but it works.

jramillete
  • 29
  • 1
  • 5
  • How are you calling that segue? Are you sure you are using the same segue id both in code and in the Storyboard? – marzapower May 27 '13 at 19:21
  • I'm not calling the segue, i'm replacing the "Login View Controller" with the "Initial View Controller" as root element in the navigation controller – jramillete May 27 '13 at 20:15

1 Answers1

7

I needed once exactly what you need now. What i did is once user did login, i set appropriate values in NSUserDefaults. Then in my AppDelegate.m, i check that NSUserDefaults values to determine if user's already login in

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

Then i set rootViewController to appropriate view controller. As you use storyboard, What i would do is to create an instance of your storyBoard as follow :

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"YourStoryBoardName" bundle:[NSBundle mainBundle]];

Then use NSUserDefaults to check user information.

       // Check user login 
      if([[NSUserDefaults standardUserDefaults] valueForKey:@"AlreadyLogin"])
        {
          // So, here user already login then set your root view controller, let's say `SecondViewController``
          SecondViewController *secondViewController = [storyBoard instantiateViewControllerWithIdentifier:@"SecondViewController"];
          // then set your root view controller 
          self.window.rootViewController = secondViewController;
        }
    else
    {
         // It means you need to your root view controller is your login view controller, so let's create it 
         LoginViewController  *loginViewController= [storyBoard instantiateViewControllerWithIdentifier:@"LoginViewController"];
         self.window.rootViewController = loginViewController;
    }

Don't forget to set your view controller identifier to appropriate values, then don't forget to clear NSUserDefaults values when user logged out.

limon
  • 3,222
  • 5
  • 35
  • 52
  • Thanks for the answer, but if i do that, the navigation controller dissapear – jramillete May 27 '13 at 23:07
  • Just wrap the new controller in a navigation controller: `UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:loginViewController]; self.window.rootViewController = navController;` – marzapower May 27 '13 at 23:37
  • 1
    I've tried to do that, and using the navigation controller from storyboard but, if i do the change in the appdelegate it works, but not from a view. It shows the navigation controller but the segues stop working. – jramillete May 28 '13 at 08:30
  • Your answer is valid when opening the application, but the first time the user login into the app i want to avoid going back in the navigation stack to the login screen again. That's the reason i need to do it in the view. – jramillete May 28 '13 at 20:19
  • It'll blow up with Storyboards. – pronebird Sep 19 '14 at 12:46