0
votes

I have a table view controller embedded in a navigation controller. The table cells are all static and selecting any of them will segue to another table view. When segue happened, the navigation bar shows 'Cancel' button for the new view, instead of 'Back' button.

I could add a back button in code like

- (void)viewDidLoad
{
    self.navigationItem.backBarButtonItem =
        [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                         style:UIBarButtonSystemItemCancel
                                        target:nil
                                        action:nil];
    self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
}

But then the back button would be a rectangle shape, not the default back button shape which has an angle on the left edge. How to simply change the cancel button to a system back button?

Here is my code for segue from table cell to the next table view

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    switch (indexPath.row) {
        case 0:
            [self performSegueWithIdentifier:@"goToSecondTable" sender:self.tableView];
            break;
        /* and perform segue for other rows */
        default:
            break;
    }
}

And there is nothing to do inside prepareForSegue.

Here is what the connections inspector showed

connections inspector

And Here is the connections for the 'Bar Button Item - Back'

Bar Button Item - Back

3
Can you post the code you're using to push the new view. And also if you are doing anything in the loading methods of the new controller?Eric
Added my code for triggering segue and loading the view. Thanks!marsant

3 Answers

0
votes

The system provided back button should be the left bar button item by default without having to do anything (in code or in IB).

Remove the connection to the backBarButton in the Connections inspector. Remove the back bar button from the nav bar in IB. Remove the outlet to the back bar button in your code. Run your app, you should see a back bar button provided for you for free.

0
votes

Why are you performing a segue in didselectrow instead of just pushing the viewcontroller?

try [self.navigationController pushViewController:YOURVIEWCONTROLLER];

that will make sure you have a back button. Also you must use a segue like that, make sure you are using a push segue to your next controller. It also looks like you created your own back button in the second viewController. You do not need to create one, as one will be created for you with the title of the previous viewcontroller. You can make sure it says back by changing self.title = @"Back" in the previous viewcontroller right before you push it.

Codes:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    switch (indexPath.row) {
        case 0:
            self.title = @"Back";
            [self.navigationController pushViewController:SECONDVC];
            break;
        /* and perform segue for other rows */
        default:
            break;
    }
}

and in viewWillAppear:

 - (void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];
   self.title = @"Whatever you want";
}
0
votes

Customize the code if don't have default back button not working

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:myBackImage style: UIBarButtonItemStylePlain target:self action:someAction];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];