0

So I was create three UITableViewControllers with UINavigationController. I want a back button on 3rd UITableViewController, what returns my view to first UITableViewController instead of second. How can I do that? That must be a real backButton, not a image or something else. Will be perfect to do this only with storyboard.

UPDATE Perhaps I poorly explained what I want. I don't want use any button with action on it. I just want something like as setting "address" of 1st TableViewController on my default back button. There is any way to do it?

  • 1
    possible duplicate of [Trying to handle "back" navigation button action in iOS](http://stackoverflow.com/questions/18824186/trying-to-handle-back-navigation-button-action-in-ios) – Kumar KL Aug 11 '14 at 09:07
  • Kumar KL, in this case we got back button with wrong title what pointed on 2nd viewController. – TChernyshenco Aug 11 '14 at 11:31

3 Answers3

0

add a button and connect it to following action

  - (IBAction)backToFirstView:(UIButton *)sender
    {
        [self.navigationController popToRootViewControllerAnimated:YES];
        (or)
        [self.navigationController popToViewController:yourFirstViewControllerObject animated:YES];
    }
Ravi
  • 2,441
  • 1
  • 11
  • 30
0

There are different ways to navigate from DetailViewController to other view controllers. We will go through the cases one by one.

  • First of all I would like to clear that if its your default navigation bar's back button, then it must return to the last most view controller only which is actually a default behavior of a navigation controller.
  • Second, If you would like to go back to the last most view controller on the tap of a button placed by you, you should write the following code
   [self.navigationController popToViewController:NAME_OF_A_VIEWCONTROLLER animated:YES];
  • Third, If you would like to go to the first view controller from where you started, you should write the following code

    [self.navigationController popToRootViewControllerAnimated:YES];

Raj
  • 413
  • 1
  • 4
  • 5
0

Ok, I found a way to resolve my problem. Thanks for your answers guys, they was very helpful. So for resolve this problem you just need use link what give me Kumar KL upper, and wrote next method in your UITableVIewController

-(void) viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
       // Navigation button was pressed. Do some stuff 
      [self.navigationController popViewControllerAnimated:NO];
    }
    [super viewWillDisappear:animated];
}

Now you got a backButton what redirect you to your viewController, BUT title of this button is wrong. Let's resolve that unfair. Create new class CustomSegueцрфе inherited from UIStoryboardSegue with next code in CustomSegue.m :

- (void)perform
{
    UIViewController *sourceView = (UIViewController *) self.sourceViewController;
    UIViewController *destinationView = (UIViewController *) self.destinationViewController;
    [[destinationView navigationItem] setTitle:@"TitleOfYourViewController" ] ;
    [sourceView.navigationItem setTitle:@"TitleOfButton"] ;
    [sourceView.navigationController pushViewController:destinationView animated:YES];
}

Now you can go to storyboard and connect 2nd ViewController with 3rd with custom segue. Like you see UINavigationController uses Title of previous ViewController for button title, so you just need change it.

Community
  • 1
  • 1