Sunny
Sunny

Reputation: 505

Switching views in iOS

I have 3 view controllers in my storyboard for example Page1, Page2, and Page3. Both Page1 and Page2 are pushed to Page3. What I want to do is when Page3tap on Back button, the view will 100% return to Page1 and never get back to Page2. The code I am using now is

[self.navigationController popViewControllerAnimated:YES];

What will go back to the page it comes from is there any way to do that? I want to fix the page it back but not pop back to where it comes from.

Upvotes: 0

Views: 137

Answers (3)

holex
holex

Reputation: 24041

you should use the

[self.navigationController popToRootViewControllerAnimated:true];

it will work for you perfectly, if the Page1 is the root.

Upvotes: 2

Madhumitha
Madhumitha

Reputation: 3824

Try this code,

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

 NSInteger currentIndex = [self.navigationController.viewControllers indexOfObject:self];
if( currentIndex-2 >= 0 ) 
{
   [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:currentIndex-2] animated:YES];
}

Upvotes: 0

Krunal
Krunal

Reputation: 6490

You should create Custom Navigation Controller with back button on it and on click of back button write my code for navigating to a page 1

drag a navigation bar in .xib file and put a back button on it,

code snippet on back button,

-(void)BackButtonClick
{
  YourView *Obj = [[YourView alloc] initWithNibName:@"YourView" bundle:nil];
  [self.navigationController popViewController:Obj animated:YES];
  [Obj release];
}

Upvotes: 0

Related Questions