简体   繁体   English

将导航栏取消按钮替换为后退按钮

[英]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. 当发生segue时,导航栏将为新视图显示“取消”按钮,而不是“返回”按钮。

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. 在prepareForSegue中没有任何事情要做。

Here is what the connections inspector showed 这是连接检查器显示的内容

连接检查器

And Here is the connections for the 'Bar Button Item - Back' 这是“ 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). 默认情况下,系统提供的后退按钮应为左栏按钮,而无需执行任何操作(使用代码或IB)。

Remove the connection to the backBarButton in the Connections inspector. 在“连接”检查器中删除与backBarButton的连接。 Remove the back bar button from the nav bar in IB. 从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? 为什么您要在didselectrow中执行序号,而不是仅按ViewController?

try [self.navigationController pushViewController:YOURVIEWCONTROLLER]; 尝试[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. 另外,您必须使用类似的segue,确保对下一个控制器使用推送segue。 It also looks like you created your own back button in the second viewController. 看起来您在第二个viewController中创建了自己的后退按钮。 You do not need to create one, as one will be created for you with the title of the previous viewcontroller. 您无需创建一个,因为将为您创建一个具有上一个viewcontroller标题的视图。 You can make sure it says back by changing self.title = @"Back" in the previous viewcontroller right before you push it. 您可以通过在推送之前在前一个视图控制器中更改self.title = @“ Back”来确保它返回。

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: 并且在viewWillAppear中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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