UINavigationController popToViewController Problem

I use the UINavigationController to control the navigation of the controllers

In the normal case

RegistrationView β†’ LoginView β†’ HomeView

From HomeView and any other controllers, one screen appears with the name "Settings", in which there is a button "Exit". When clicking this button, the screen will be displayed in LoginView in the usual 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]; } } } 

As soon as the user is registered, and if the user has logged in once, the application has an autoload function. Therefore, at that time, LoginView will not appear in the navigation list. Therefore, in this scenario, the above code does not work. Therefore, at that time I can not enter LoginView. I need help solving this problem. thanks in advance

+6
source share
2 answers

If you do not have an instance of LoginViewController in the navigation stack, just create it:

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

Then you can use the viewControllers UINavigationController property. You can replace the current view controller with loginController or insert loginController at a given index and click on it.

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

try it

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

Source: https://habr.com/ru/post/947812/


All Articles