简体   繁体   English

导航栏后退按钮文本未更改

[英]Navigation bar back button text is not changing

I am trying to change the text of the navigation bar back button text. 我正在尝试更改导航栏后退按钮文本的文本。 I have tried the solutions offered here but I cant get the text I want on back button. 我已经尝试过这里提供的解决方案,但无法在“后退”按钮上得到我想要的文本。 In appdelegate I set these features: 在appdelegate中,我设置了以下功能:

 [[UINavigationBar appearance] setBarTintColor:[self colorWithHexString:@"e74c3c"]];
 [[UINavigationBar appearance] setBarTintColor:[self colorWithHexString:@"e74c3c"]];   

[[UINavigationBar appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIColor whiteColor], UITextAttributeTextColor,
  [UIFont fontWithName:@"Futura-CondensedMedium" size:19.0], UITextAttributeFont,nil]];

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor whiteColor];

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
 setTitleTextAttributes:
 @{NSForegroundColorAttributeName:[UIColor whiteColor],
   NSShadowAttributeName:shadow,
   NSFontAttributeName:[UIFont fontWithName:@"Futura-CondensedMedium" size:19.0]
   }
 forState:UIControlStateNormal];

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

And in every viewcontroller's viewdidload I set: 并且在每个viewcontroller的viewdidload设置:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"SomeText" style:UIBarButtonItemStylePlain target:nil action:nil];

or 要么

UIBarButtonItem *newBackButton = 
        [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];

Any suggestions? 有什么建议么?

If I haven't misunderstood you you can do this to set the title of the back button. 如果我没有误会,您可以设置返回按钮的标题。 Suppose there are three view controllers: A , B and C . 假设有三个视图控制器: ABC Now you want the title of the back button to be "MyViewA" when you navigate from A to B then you need to put this code in "viewWillAppear:" of your ViewController A (this will show the title of the back button as "MyViewA" when you are on view controller B ). 现在,当您从A导航到B时,您希望后退按钮的标题为“ MyViewA”,然后需要将此代码放在ViewController A “ viewWillAppear:”中(这会将后退按钮的标题显示为“ MyViewA” “当您在视图控制器B )。

 //viewWillAppear of ViewController A
 -(void)viewWillAppear:(BOOL)animated{

       //now set title of back button
       self.navigationItem.backBarButtonItem =
       [[UIBarButtonItem alloc] initWithTitle:@"YourTitle"
                                  style:UIBarButtonItemStyleBordered
                                 target:nil
                                 action:nil];
 }

Likewise if you are navigating from B to C and want to set the back button title for ViewController C , you need to put the above code in "viewWillAppear:" delegate of ViewController B . 同样,如果您要从B导航到C并且想为ViewController C设置后退按钮标题,则需要将以上代码放入ViewController B “ viewWillAppear:”委托中。 In short you need to set the title of the next view controller on your current view controller. 简而言之,您需要在当前视图控制器上设置下一个视图控制器的标题。

You can't change the value of the text in the backbutton. 您无法在后退按钮中更改文本的值。 You have to set the button as the left or right barButtonItem (probably left if you want it as back button) like this: 您必须将按钮设置为左侧或右侧barButtonItem(如果希望将其作为后退按钮,则可能为左侧),如下所示:

[self.navigationItem setLeftBarButtonItem:newBackButton];

you can then attach an IBAction to the button by setting it when you init your button: 然后,您可以通过在设置按钮时设置IBAction来将其附加到按钮上:

UIBarButtonItem *newBackButton = 
    [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
                                     style:UIBarButtonItemStyleBordered 
                                    target:nil 
                                    action:yourBackFunction];

at least that would be a workaround and I have also used it before... 至少这是一种解决方法,我之前也曾使用过...

The back button belongs to the previous view controller, not the one currently presented on screen. 后退按钮属于上一个视图控制器,而不是当前在屏幕上显示的那个。 To modify the back button you should update it before pushing, on the view controller that initiated the segue: 要修改后退按钮,您应该在启动segue的视图控制器上在按下按钮之前对其进行更新:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Delete back item title in next controller
    UIBarButtonItem *newBackButton =
    [[UIBarButtonItem alloc] initWithTitle:@"New Title"
                                     style:UIBarButtonItemStylePlain
                                    target:nil
                                    action:nil];
    [[self navigationItem] setBackBarButtonItem:newBackButton];
}

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

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