Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 686
Jignesh Fadadu Avatar asked Jun 21 '13 07:06

Jignesh Fadadu


2 Answers

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];
like image 61
Michał Ciuba Avatar answered Nov 11 '22 20:11

Michał Ciuba


Try This

if ([self.navigationController.viewControllers containsObject:objLogin]) {
            [self.navigationController popToViewController:objLogin animated:TRUE];
        }
        else {
            [self.navigationController pushViewController:objLogin animated:TRUE];
        }
like image 40
Sunny Shah Avatar answered Nov 11 '22 20:11

Sunny Shah