简体   繁体   中英

Replace navigation bar cancel button with back button

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

连接检查器

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

条形按钮项-返回

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.

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];

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM