Question

I have four view controller classes A, B, C, D.

Class D is pushed when the "go" button is pressed from Class A & B.

I have a method in the app delegate which does that for me

-(void)showViewController {
    self.ViewController = [[[ViewController alloc] initWithNibName:@"View"     bundle:nil] autorelease];
    [self.navigationController pushViewController:self.ViewController animated:YES];
}

When the "back" button is pressed from Class D it pops back to either class A & Class B depending on which class pushed Class D

- (void)popViewController {
    [self.navigationController popViewControllerAnimated:YES];
}

What I want to implement is suppose if Class A pushes Class D but when the "back" button is pressed in Class D i want it to go to Class C instead of Class A

I have tried to implement the following code

-(void)popToViewController{
    [self.navigationcontroller popToViewController:self.ClassCViewController animated:YES]
}

This causes changes in Class B which i do not want.

Any Suggestions ?

Was it helpful?

Solution

So what I would suggest to you is that you take some time and read up on the apple documentation concerning delegates. They will do exactly what you want here where two views can have the same child view. From there have the child view call the delegate to figure out what it needs to do.

Another thing to keep in mind is that it is good practice to have any view that pushes a view to also be the one responsible for popping that view.

EDIT: Just to help you with your original question and get you thinking down the right path. When the back button is pressed in Class D. You would want it to call a method on the delegate (which could be either class A or Class B) from there you could make it go to either the same class. Or if you want to present another view have the delegate view present Class C. Using delegates allows you to have Class A present Class C while allowing Class B to just stay at B when back is pressed. I hope this answers your question.

OTHER TIPS

when u pushes ur ViewController in a navigation Controller then all ViewControllers are pushed inside the navigation controller.

find out the viewController from the navigation controller stack and pop to that selected view controller.

Suppose u navigate from A-> B -> C -> D. Then if you normally pop ur view controller from Class D using [self.navigationController popViewControllerAnimated:YES];

then it pops to Class C.

But if you want to pop to Class B or Class A use this:

-(void)popToSelectedViewController
{
  //get all view Controller from Stack of navigation controller
    NSArray *viewControllers=[self.navigationController viewControllers];

  //Declare an Object your selected class
    B  *homeVC=nil;

  //Find out you selected class where u want to pop
    for (int i=0; i<[viewControllers count]; i++)
    {
        UIViewController *tempVC=[viewControllers objectAtIndex:i];
        if([tempVC isKindOfClass:[B class]])
        {
            homeVC=[viewControllers objectAtIndex:i];
            break;
        }
    }


//pop to that selected class if found in stack otherwise popTo RootView
    if(homeVC)
        [self.navigationController popToViewController:homeVC animated:YES];
    else
        [self.navigationController popToRootViewControllerAnimated:YES];

}

I hope this will helps you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top