0

I created a RootViewController in my app delegate like so:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    StartViewController *viewController = [[StartViewController alloc] init];
    StartNavigationController *navigationController=[[StartNavigationController alloc] initWithRootViewController:viewController];
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
}

When I hit the logout button, I want to send the user back to the rootview controller:

- (IBAction) logoutButtonPressed:(UIButton *)sender
{
    [Users logOut];

    [self.navigationController popToRootViewControllerAnimated:YES];
}

This works fine when I run it on my iPhone 4s (testing iphone 6 when it arrives), but if I leave the user logged in for more than a day and click the logout button, the screen slides to black.

Why is my root view controller not calling my startviewcontroller after like 24 hours or so?

holroy
  • 3,047
  • 25
  • 41
cdub
  • 24,555
  • 57
  • 174
  • 303
  • anyone got any advice? – cdub Nov 10 '14 at 08:01
  • I suspect the viewcontroller is deallocated? Try put for(UIViewController *controller in self.navigationController.viewControllers){ NSLog(@"Class %@",NSStringFromClass([controller class])); } in logout function before poping out. See if it exists – iphonic Nov 10 '14 at 08:05
  • Deallocation would be my guess too. How can I just create the StartViewController *viewController = [[StartViewController alloc] init]; and add it to the self.navigationcontroller? Do I check if it's nil? – cdub Nov 10 '14 at 20:28
  • See my answer, I think this way we can achieve this. – iphonic Nov 11 '14 at 04:48

1 Answers1

1

I am trying to put an answer, I guess it should work for you. First you check for StartViewController whether it exist in navigationController stack or, not. As per your stack StartViewController should be your first controller added in navigationController.

StartViewController *loginController=[self.navigationController.viewControllers objectAtIndex:0];


if(loginController){
   [self.navigationController popToViewController:loginController animated:YES];
}else{
    NSMutableArray *controllers=[[NSMutableArray alloc] init];

   loginController=[[StartViewController alloc] initWithNibName:@"StartViewController" bundle:nil];
    [controllers addObject:loginController];
    [controllers addObjectsFromArray:self.navigationController.viewControllers];
    self.navigationController.viewControllers=[[NSArray alloc] initWithArray:controllers];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

I think it should work.

Cheers.

iphonic
  • 12,615
  • 7
  • 60
  • 107
  • Seems logical so we shall see when I wake up manana if my app works fine or not. Does the simulator do anything funky, like deallocate stuff differently? I'm running this app on my iphone 4s device. – cdub Nov 11 '14 at 08:11