[英]set title of back bar button item in ios
I am using storyboards in my application, and if I set the title property of a ViewController class, that string will appear as the text of my back button when I push a SecondViewController, how can I change this?, I want to put a different title for the back button. 我在应用程序中使用情节提要,如果我设置ViewController类的title属性,那么在按下SecondViewController时,该字符串将作为后退按钮的文本出现,我该如何更改呢?后退按钮的标题。
I have used this but it doesn't work: 我已经用过了,但是不起作用:
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
[self.navigationItem setBackBarButtonItem: btnBack];
please help me 请帮我
Try this , 尝试这个 ,
Objective-C: 目标C:
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
self.navigationController.navigationBar.topItem.backBarButtonItem=btnBack;
Swift: 迅速:
var btn = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: "backBtnClicked")
self.navigationController?.navigationBar.topItem?.backBarButtonItem=btn
When you use Storyboards you can change the text of the left bar button item aka the back button with no code. 使用情节提要时,无需任何代码即可更改左栏按钮项目(也称为后退按钮)的文本。 You will obviously need a
UINavigationController
within your Storyboard so you have the ability to push and pop views. 显然,您的情节提要中将需要一个
UINavigationController
,以便能够推送和弹出视图。
Then when a view is pushed from this view it will use this title set. 然后,当从该视图推送视图时,它将使用此标题集。
Set a backBarButtonItem to the navigationItem of the previous viewController. 将backBarButtonItem设置为上一个viewController的navigationItem。 Setting the topItem only works on the first time viewWillAppear.
设置topItem仅在第一次viewWillAppear上起作用。 Please check this post for detail: iOS Set Navigation Bar Back Button Title
请查看此帖子以获取详细信息: iOS设置导航栏后退按钮标题
NSArray *viewControllerArray = [self.navigationController viewControllers];
// get index of the previous ViewContoller
long previousIndex = [viewControllerArray indexOfObject:self] - 1;
if (previousIndex >= 0) {
UIViewController *previous = [viewControllerArray objectAtIndex:previousIndex];
previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:backButtonTitle
style:UIBarButtonItemStylePlain
target:self
action:nil];
}
尝试在将第二个视图控制器推入的位置更改第一个视图控制器的标题,并在弹出时将标题设置为旧名称。
Try this: 尝试这个:
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(backButtonClicked:)];
[self.navigationItem setLeftBarButtonItem: btnBack];
-(void)backButtonClicked:(UIBarButtonItem*)sender
{
//do ur stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.