Question

Lets say i have ViewController A with 2 buttons, each is segueing to ViewController B and C (one button to View B and other to C).From ViewController C I segueing to ViewController D. All the ViewControllers are with Navigation bar so from view B i can return to A, and from view D i can return to D->C->A. The problem is when i segueing between view D to B: the segue is performed but now in the navigation bar of B i retuned to view D and i want B will return to A like it should. what is the solution????

Was it helpful?

Solution

This can be done using two ways as far as i know.

1) Use a custom back button on D controller

2) Change the stack of navigationController i.e NSArray of self.navigationController.viewControllers in the class of D controller

Use Custom back Button

You can do it as follows in D controller.This is valid only if A controller is the rootViewController of navigationController.If you are keeping track of your A controller than you can use the following also

  • (NSArray *)popToViewController:(UIViewController )viewController animated:(BOOL)animated

    - (void)viewDidLoad
    {
    [super viewDidLoad];
     UIBarButtonItem *backButton = [[UIBarButtonItem alloc]  initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(goBack:)];
     self.navigationItem.leftBarButtonItem = backButton;
      self.navigationItem.hidesBackButton = YES;
    }
    
    -(void)goBack:(id)sender
    {
    [self.navigationController popToRootViewControllerAnimated:YES];
    

    }

Change the stack of navigationController

This is useful if you want to navigate to a particular controller.But the back button still displays after getting back to A controller from B,that you might have to see yourself by making some changes.Don't try to change the navigation stack in prepareForSegue

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableArray *controllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    NSMutableSet *controllersToRemove = [NSMutableSet new];
        for (id viewController in controllers) {
            if (![viewController isKindOfClass:[self class]]&&![viewController isKindOfClass:[NRViewController class]]) {
                    [controllersToRemove addObject:viewController];
                }
        }

        for (id controller in controllersToRemove) {
            [controllers removeObject:controller];
        }
    self.navigationController.viewControllers = controllers;
}

Edited Code for custom back button

If you want the original back Button

You have to use image for that with code as follow

UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 60.0f, 30.0f)];
    UIImage *backImage = [[UIImage imageNamed:@"back_button_normal.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12.0f, 0, 12.0f)];
    [backButton setBackgroundImage:backImage  forState:UIControlStateNormal];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backButtonItem;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top