1

I've got 2 viewControllers A and B.

From controller A i want to push to controller B, and it;s work great.

Now in controller B I've got 'plus' button which push me to another B controller (the same ViewController). Now when im in secontr B viewController when i push back button i should be poped to A viewController. So i want to know how to remove ViewControler when i push another viewController?

Situation looks like this:

A (push B) -> B (push B) -> B (push B) -> B (pop B) -> A

Thanks for help.

edzio27
  • 4,126
  • 7
  • 33
  • 48

5 Answers5

2

Try this,

id tempViewCont=nil;
for(id viewCont in [self.navigationController viewControllers])
{
   if([viewCont isKindOfClass:[A class]])
   {
      tempViewCont=viewCont;
      break;
   }
}

if(tempViewCont)
{
  [self.navigationController popToViewController:tempViewCont animated:yes]; 
}
Ishu
  • 12,797
  • 5
  • 35
  • 51
  • 1
    @edzio27: Also if you have multiple A controllers in stack & you want to pop to later pushed A then you need to traverse loop in reverse order(You need to use for/while loop). – Rahul Wakade Jan 24 '13 at 13:16
  • Thanks Ishu, for this. I also find help in this topic http://stackoverflow.com/questions/2099319/how-to-remove-a-particular-view-controller-from-uinavigationcontroller-stack – edzio27 Jan 24 '13 at 13:46
1

Solution will be in popToRootViewController: Call this method when you're done on B3 controller

So it will be: A (pushViewController B1) -> B1 (pushViewController B2) -> B2 (pushViewController B3) -> B3 (popToRootViewController:) -> A

Is that helps you?

Yanny
  • 490
  • 4
  • 16
1

Just try

 [self.navigationController popToRootViewControllerAnimated:YES];

Or

  [self.navigationController popToViewController:A viewcontroller animated:yes];
0

You can try the following if you are pushing view controller A only once:

NSArray *tempArray = [self.navigationController viewControllers];
for (id viewController in tempArray) {
    if ([viewController isKindOfClass:A]) {
        [self.navigationController popToViewController:viewController animated:yes];
        break;
    }
}

Hope this helps.

Nameet
  • 650
  • 8
  • 20
  • +1 But, like the accepted answer, this finds the first occurrence of `A` and if you need the last, you'd have to tweak. And obviously if you wanted the first one, and that was the initial view controller, then you'd just do `popToRootViewControllerAnimated`. – Rob Jan 24 '13 at 14:31
0

While all of the other answers are good, in my mind the question is whether you should be pushing from B to another copy of itself at all. For me, this is answered by the question of whether you ever want to pop from one B to the preceding B. If so, that's fine and use one of the other popToRootViewControllerAnimated or popToViewController answers when you want to go back to A.

If not, you simply shouldn't be pushing from B to another copy of itself at all. What you could do, though, if you want to present the user to demonstrate "let's do that again" button, you might use transitionWithView. For example, if we imagine that you have a bunch of UITextField controls on B and you have a "Add Another" button, it might do the following:

- (IBAction)onPressAddButton:(id)sender
{
    // save the record here

    // now, let's animate the resetting of the text fields

    [UIView transitionWithView:self.view
                      duration:0.5
                       options:UIViewAnimationOptionTransitionCurlUp
                    animations:^{
                        self.textField1.text = nil;
                        self.textField2.text = nil;
                        self.textField3.text = nil;
                    }
                    completion:nil];
}

And, by the way, if you do it this way, you now don't have to do anything fancy to get back from B back to A. The standard navigation bar does everything we need.

Rob
  • 415,655
  • 72
  • 787
  • 1,044