ios 如何更改 uinavigationbar 后退按钮文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7237826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change uinavigationbar back button text
提问by C.Johns
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.
我目前正在尝试更改加载 tablecell 触摸的子视图的后退按钮文本。然而,即使我试图实现它,它仍然在后退按钮中显示父母的标题。
I am trying to load a new value into the back button inside viewdidload method like so
我正在尝试将一个新值加载到 viewdidload 方法中的后退按钮中
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = @"Back";
self.navigationItem.backBarButtonItem = myBarButtonItem;
[myBarButtonItem release];
however its not working.
但是它不起作用。
回答by Juanjo
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:
您需要更改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];
}
回答by Hung Tran
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()
这是文档不太清楚的地方,直到您阅读并重新阅读并使用每个设置。
要更改默认后退按钮的标题,请将此行添加到viewDidLoad()
self.navigationController.navigationBar.topItem.title = @"Your Label";
If you want the button to be invisible - then set the value to @""
empty string.
如果您希望按钮不可见 - 则将该值设置为@""
空字符串。
回答by C.Johns
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.
好吧想通了,我在父视图 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];
}
}
回答by Peter
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.
使用 Xcode 5 更改后退按钮名称的最佳方法是在后退按钮将返回到的视图控制器上的导航项的 IB 中编辑后退按钮字段。例如,如果您有一个转到详细信息屏幕的列表 TableViewController,请更改列表 TableViewController 的导航项(在 IB 中)上的后退按钮,以更改详细信息屏幕显示的后退按钮名称。
回答by sha
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.
它不会像你想要的那样工作。导航按钮将始终具有上一个视图的标题。您可以做什么 - 在推送新视图之前更改第一个视图的标题。这是我能找到的唯一解决相同问题的方法。
回答by mashern
After viewDidLoad
在 viewDidLoad 之后
self.navigationController!.navigationBar.backItem?.title = "Back"
self.navigationController!.navigationBar.backItem?.title = "Back"
回答by gamal
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
回答by Hermann Klecker
Your code looks fine so far.
到目前为止,您的代码看起来不错。
Is this code executed before the
这段代码是否在执行之前执行
[super viewDidLoad];
statement (wrong) or after it (good)?
声明(错误)或之后(好)?
回答by Ali
This articleshould 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.
正如你在上面看到的,你需要做的就是设置控制器的 leftBarButtonItem ,它会隐藏后退按钮。选择器 handleBack 现在处理返回事件,然后您需要手动将视图控制器从 UINavigationController 的堆栈中弹出。你可以在那里实现任何你自己的代码,甚至阻止离开页面。
回答by T.J.
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.
此标题将放置在子视图的后退按钮中。重要的一点是这个标题在子视图中用作父视图的后退按钮。