1
votes

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.

5

5 Answers

2
votes

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]; 
}
1
votes

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?

1
votes

Just try

 [self.navigationController popToRootViewControllerAnimated:YES];

Or

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

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.

0
votes

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.