0
votes

I have 4 views.

1st is rootView and then View1, View2 & View3.
It is working according rootView-> View1-> View2-> View3 .

I have to jump from View1 to View3 but when I use back button from View3, it pops to View2 . If I use following code

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];

it does not work cause of indexing, at this time View3 has index value 2 when I push the View3 from View 1.

So is there any other method to pop View2 from View3 using back button?

3
It's not clear what you want jump from View1 to View3 needs pushing or pop View2 from View3 needs poping. - iPhone Programmatically

3 Answers

0
votes

Create new class MYViewController : UIViewController with initWithBackButton:(UIBarButtonItem *)backButton;

@property (nonatomic, strong) UIBarButtonItem *saveBackButton

- (id)initWithBackButton:(UIBarButtonItem *)backButton {

  self = [super init];

  if(self){
      self.saveBackButton = backButton;
  }
  return self;
}

in root

Create 3 property vc1, vc2,vc3;

when you create MYViewController

{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, width, height);
    [button addTarget:self action:@selector(iWantToOpenNumber2:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    self.vc3 = [[MYViewController alloc] initWithBackButton:backItem];
}

- (void)iWantToOpenNumber2:(id)sender {
    [self dismissViewControllerAnimated: YES completion: ^{
       [self presentViewController:self.vc2 animated: YES completion:^{}]; 
    }];

}
0
votes

Import the view1 in view3 then create an instance for view1 and paste this code

UIViewController *view1Reference = [[UIViewController alloc] init];
[self.navigationController popToViewController:view1Reference animated:YES];
0
votes

via back button. (for specific index)

NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:2] animated:NO];

(for previous view)

[self.navigationController popViewControllerAnimated:YES];