如何更改 uinavigationbar 后退按钮文本

发布于 2024-12-02 04:12:52 字数 338 浏览 0 评论 0原文

我目前正在尝试更改加载表格单元格触摸的子视图的后退按钮文本。然而,即使我尝试实现它的方式,它仍然在后退按钮中显示父母的标题。

我正在尝试将新值加载到 viewdidload 方法内的后退按钮中,

UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = @"Back";
self.navigationItem.backBarButtonItem = myBarButtonItem;
[myBarButtonItem release];

但它不起作用。

I am currently trying to change the back button text of a subview that is loaded of a tablecell touch. However even with the way I am trying to implement it it still shows the parents title in the back button.

I am trying to load a new value into the back button inside viewdidload method like so

UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = @"Back";
self.navigationItem.backBarButtonItem = myBarButtonItem;
[myBarButtonItem release];

however its not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

狠疯拽 2024-12-09 04:12:52

您需要从以前的视图更改 self.navigationItem.backBarButtonItem ,而不是当前视图(我知道,这似乎有点不合逻辑)。例如,在表视图中,您可以执行以下操作:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:@"My title"];
    UIBarButtonItem *boton = [[UIBarButtonItem alloc] initWithTitle:@"Custom back button text" style:UIBarButtonItemStyleBordered target:self action:@selector(mySelector:)];
    self.navigationItem.backBarButtonItem  = boton;
    [boton release];
}

You need to change self.navigationItem.backBarButtonItem from previous view, not current view (I know, it seems to be a little bit illogical). For example, in your table view you can do the following:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:@"My title"];
    UIBarButtonItem *boton = [[UIBarButtonItem alloc] initWithTitle:@"Custom back button text" style:UIBarButtonItemStyleBordered target:self action:@selector(mySelector:)];
    self.navigationItem.backBarButtonItem  = boton;
    [boton release];
}
还在原地等你 2024-12-09 04:12:52

在您阅读并重新阅读并尝试每个设置之前,文档并不那么清晰。
要更改默认后退按钮的标题,请在 viewDidLoad() 中添加此行。

self.navigationController.navigationBar.topItem.title = @"Your Label";

如果您希望按钮不可见,请将值设置为 @"" 空字符串。

This is where the documentation is not so clear until you have read and re-read and play around with each settings.
To change the title of the default back button add this line in viewDidLoad()

self.navigationController.navigationBar.topItem.title = @"Your Label";

If you want the button to be invisible - then set the value to @"" empty string.

烟─花易冷 2024-12-09 04:12:52

好吧,弄清楚了,我在父视图 tableView:didSelectRowAtIndexPath: 方法中发布了这段代码。这就是我的表格的外观,用户可以选择多个表格单元格。希望这有帮助。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    if (indexPath.section == 0) { //--- picks (first) section

     ChildViewController *childViewController = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:childViewController animated:YES];
    //--- this sets the back button to "Back" every time you load the child view.
     self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil] autorelease];

        if(indexPath.row == 0) { //--- picks row
            childViewController.title = @"Bike";
        }
        if(indexPath.row == 1) {
            childViewController.title = @"Model";
        }
        [vehicleSearchResponseTableViewController release];
    }


}

Okay figured it out, I posted this code in the parent views tableView:didSelectRowAtIndexPath: method. this is how my one looks with multiple tablecells the user can select. Hope this helps.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    if (indexPath.section == 0) { //--- picks (first) section

     ChildViewController *childViewController = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:childViewController animated:YES];
    //--- this sets the back button to "Back" every time you load the child view.
     self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil] autorelease];

        if(indexPath.row == 0) { //--- picks row
            childViewController.title = @"Bike";
        }
        if(indexPath.row == 1) {
            childViewController.title = @"Model";
        }
        [vehicleSearchResponseTableViewController release];
    }


}
辞慾 2024-12-09 04:12:52

使用 Xcode 5 更改后退按钮名称的最佳方法是编辑后退按钮将返回到的视图控制器上导航项的 IB 中的后退按钮字段。例如,如果您有一个转到详细信息屏幕的列表 TableViewController,请更改列表 TableViewController 的导航项(在 IB 中)上的后退按钮,以更改详细信息屏幕显示的后退按钮名称。

The best way with Xcode 5 to change the back button name is to edit the Back Button field in IB of the Navigation Item on the View Controller to which the back button will return. For example, if you have a list TableViewController that goes to a detail screen, change the Back Button on the list TableViewController's Navigation item (in IB) to change the back button name that the detail screen displays.

最舍不得你 2024-12-09 04:12:52

它不会按照你想要的方式工作。导航按钮将始终具有上一个视图的标题。但你可以做什么 - 在推送新视图之前更改第一个视图的标题。这是我能找到解决同样问题的唯一方法。

It's not gonna work the way you're trying to do. Navigation button will always have title of previous view. What you can do though - change title of first view before pushing the new one. This is the only was I could find to solve same problem.

独夜无伴 2024-12-09 04:12:52

到目前为止,您的代码看起来不错。

这段代码是在

[super viewDidLoad];

语句之​​前执行(错误)还是在语句之​​后执行(好)?

Your code looks fine so far.

Is this code executed before the

[super viewDidLoad];

statement (wrong) or after it (good)?

简单爱 2024-12-09 04:12:52

viewDidLoad之后

self.navigationController!.navigationBar.backItem?.title = "返回"

After viewDidLoad

self.navigationController!.navigationBar.backItem?.title = "Back"

习惯成性 2024-12-09 04:12:52

我的建议是向家长页面标题栏添加一个单独的标签。
对我来说效果很好。

let titleLabel = UILabel(frame: CGRect(x: (view.frame.width * (3/8)), y: 0, width: (view.frame.width * 0.25 ), height:30))
        titleLabel.text = "Title"
        titleLabel.textAlignment = .center
        titleLabel.textColor = UIColor.white
        titleLabel.font = UIFont.systemFont(ofSize: 20)
        navigationItem.titleView = titleLabel 

My suggestion was to add a separate Label to parents Page title Bar.
Worked fine for me.

let titleLabel = UILabel(frame: CGRect(x: (view.frame.width * (3/8)), y: 0, width: (view.frame.width * 0.25 ), height:30))
        titleLabel.text = "Title"
        titleLabel.textAlignment = .center
        titleLabel.textColor = UIColor.white
        titleLabel.font = UIFont.systemFont(ofSize: 20)
        navigationItem.titleView = titleLabel 
池予 2024-12-09 04:12:52

这篇文章应该可以满足您的需求。

来自作者:

正如您在上面看到的,您需要做的就是设置控制器的 leftBarButtonItem,它将隐藏后退按钮。选择器handleBack现在处理后退事件,然后您需要手动将视图控制器从UINavigationController的堆栈中弹出。您可以在那里实现您自己的任何代码,甚至阻止离开页面。

This article should do what you want.

From the author:

As you can see above, all you need to do is set the leftBarButtonItem of the controller and it will hide the back button. The selector handleBack now handles the back event and you need to then manually pop the view controller off the UINavigationController’s stack. You can implement any of your own code in there, even blocking leaving the page.

谁与争疯 2024-12-09 04:12:52

一个好方法是在调用子视图控制器之前在父视图控制器中设置后退按钮的标题,如下所示:

[self.navigationItem.backBarButtonItem setTitle:@"Your Custom Title"];

此标题将放置在子视图上的后退按钮中。这里重要的一点是该标题在子视图中用作父视图的后退按钮。

A good way to do this is to set the title of the back button in the parent view controller before calling the child like this:

[self.navigationItem.backBarButtonItem setTitle:@"Your Custom Title"];

This title will be placed in the back button on the child view. The important point to get here is this title is used in the child view as the back button to the parent view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文