How to go to a specific view?

I have 3 views (xib'd), the third view opens the modal view (also xib'd). My goal is to get rid of the modal view and go to view # 1.

I have used the following code but it does nothing.

self.statusView = [[StatusViewController alloc] initWithNibName:@"StatusViewController" bundle:nil];
[self.navigationController popToViewController:self.statusView animated:YES];
[self.navigationController popToViewController: 

      

I have also tried the following, the same result. [self.navigationController.viewControllers objectAtIndex: 0] animated: YES];

I'm going crazy...

statusView has an accessory that is synthesized regularly and is the view I want to navigate to.

+2


source to share


2 answers


It is not entirely clear how your views are aligned relative to each other based on what you have said so far.

I am assuming you have a navigation controller and 3 view controllers that are displayed in the navigation stack.

If so, and you want to go back two screens at the same time (# 3 to # 1, skipping # 2), you need a pointer to the view controller for # 1 (not the view) .It looks like the first method call popViewController:

in your question sends a view.

Sample code to map to the first view controller:

UINavigationController* navController = self.navigationController;
UIViewController* controller = [navController.viewControllers objectAtIndex:0];
[navController popToViewController:controller animated:YES];

      



If you've tried this and it didn't work, there are several things that can go wrong:

  • It may self.navigationController

    not actually be the correct object.
  • Perhaps the view you are expecting does not actually render # 0 on the navigation stack.
  • The navigation controller you are working with may not currently be displayed.

Here are some additional steps you can take to test these hypotheses:

  • When you first assign the navigation controller you want to work with, call NSLog(@"Nav controller is at %p", navController);

    and in that code add a call NSLog(@"Now my navController is at %p", navController);

    and check for address matching.
  • If the navigation controller is correct, print the current navigation stack; something like this (which assumes each view controller has a different class name):

    for (UIViewController* viewController in navController.viewControllers) {
      NSLog(@"%s", class_getName([viewController class]));
    }
    
          

  • Do something visual for the navigation controller that you think is visible to make sure it is in fact. For example, [navController.visibleViewController.view addSubview:aColorFulView];

    where it aColorFulView

    is visually obvious UIView

    .

+7


source


-popToViewController allows you to pop controllers from the stack. What you want to do is push the new viewControllers onto the stack, so use:



[self.navigationController pushViewController: self.statusView animated: YES];

+1


source







All Articles