Jignesh Fadadu
Jignesh Fadadu

Reputation: 849

UINavigationController popToViewController issue

I am using UINavigationController to deal with controllers navigation

In Normal Case

RegistrationView -> LoginView -> HomeView

From HomeView & any other next controllers there is one screen called Setting is opened up which is having Logout Button. On the click of this button screen will be popped to the LoginView in normal case.

- (IBAction)btnLogoutSelected:(id)sender
{
    NSArray *navArr=self.navigationController.viewControllers;
    for (UIViewController *nav in navArr)
    {
        if ([nav isKindOfClass:[LoginViewController class]])
        {
            [self.navigationController popToViewController:nav animated:YES];
        }
    }
}

Once User will be registered & If user Logged in once, here application is having autoLogin functionality. So at that time LoginView will not be in the Navigation count. So in this scenario above code is not working. So at that time I am not able to go to the LoginView. I need help to solve this issue. Thanks in advance

Upvotes: 6

Views: 8288

Answers (2)

Michał Ciuba
Michał Ciuba

Reputation: 7944

In case you don't have an instance of LoginViewController on navigation stack, simply create it:

    LoginViewController* loginController = [[LoginViewController alloc] init]; //use appropriate initWith... method

Then you can use viewControllers property of UINavigationController. You can replace current view controller with loginController, or insert loginController at given index and pop to it.

NSMutableArray* newViewControllers = [self.navigationController.viewControllers mutableCopy];
[newViewControllers replaceObjectAtIndex:[newViewControllers indexOfObject:self] withObject:loginController];
[self.navigationController setViewControllers:newViewControllers animated:YES];

Upvotes: 8

Sunny Shah
Sunny Shah

Reputation: 13020

Try This

if ([self.navigationController.viewControllers containsObject:objLogin]) {
            [self.navigationController popToViewController:objLogin animated:TRUE];
        }
        else {
            [self.navigationController pushViewController:objLogin animated:TRUE];
        }

Upvotes: 2

Related Questions