iOS >> UINavigation 项后退按钮标题不会改变

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18445587/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:52:33  来源:igfitidea点击:

iOS >> UINavigation Item Back Button Title Doesn't Change

iosback-buttonuinavigationitem

提问by Ohad Regev

I'm trying to set the BACK button for a pushed VC set within a UINavigationController stack. I use the following code, and it's not working - I still get the previous VC name appearing as the back button title.

我正在尝试为 UINavigationController 堆栈中的推送 VC 集设置 BACK 按钮。我使用以下代码,但它不起作用 - 我仍然得到以前的 VC 名称显示为后退按钮标题。

-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    self.title = @"VC Title";

    UIBarButtonItem* myBackButton = [[UIBarButtonItem alloc]
                                     initWithTitle:@"Back"
                                     style:UIBarButtonItemStyleBordered
                                     target:nil
                                     action:nil];

    self.navigationItem.backBarButtonItem = myBackButton;

}

Anyone?

任何人?

回答by Michal Gumny

in parent view controller:

在父视图控制器中:

- (void)viewWillDisappear:(BOOL)animated
{
    self.title = @"Back";
}

- (void)viewWillAppear:(BOOL)animated
{
    self.title = @"Title of your navigation bar";
}

Will do the trick

会做的伎俩

回答by chris13

Try setting the title in the parent view controller's viewDidLoad

尝试在父视图控制器中设置标题 viewDidLoad

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(popView)];

self.navigationItem.leftBarButtonItem = customBarItem;

回答by johnyu

From Apple's documentation:

来自苹果的文档:

The bar button item on the left side of the navigation bar allows for navigation back to the previous view controller on the navigation stack. The navigation controller updates the left side of the navigation bar as follows:

导航栏左侧的栏按钮项允许导航回到导航堆栈上的前一个视图控制器。导航控制器更新导航栏左侧如下:

If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the leftBarButtonItem property of the view controller's navigation item.

如果新的顶级视图控制器有一个自定义的左栏按钮项,则显示该项。要指定自定义左栏按钮项,请设置视图控制器导航项的 leftBarButtonItem 属性。

If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller has a valid item in its backBarButtonItem property, the navigation bar displays that item.

如果顶级视图控制器没有自定义左栏按钮项,但前一个视图控制器的导航项在其 backBarButtonItem 属性中有一个有效项,则导航栏会显示该项。

If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the stack. (If there is only one view controller on the navigation stack, no back button is displayed.)

如果任一视图控制器未指定自定义栏按钮项,则使用默认后退按钮并将其标题设置为前一个视图控制器的 title 属性的值 - 即视图控制器上一层堆栈。(如果导航堆栈上只有一个视图控制器,则不会显示后退按钮。)

Hope this helps.

希望这可以帮助。

回答by iOSTsunami

self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:@"Put Any Title"
                                      style:UIBarButtonItemStyleBordered
                                     target:nil
                                     action:nil];

回答by situee

Set a backBarButtonItem to the navigationItem of the previous viewController. Check this answer https://stackoverflow.com/a/25680043/111277. Check my blog post iOS Set Navigation Bar Back Button Titlefor detail analysis.

将 backBarButtonItem 设置为前一个 viewController 的 navigationItem。检查这个答案https://stackoverflow.com/a/25680043/111277。查看我的博客文章iOS 设置导航栏返回按钮标题以获得详细分析。

回答by Akshit Zaveri

One more solution, which is very quick.

另一种解决方案,非常快。

Override this method in your Base view controller and you will have back button on every pushed view controller. (Just do not add [super setTitle:title])

在您的基本视图控制器中覆盖此方法,您将在每个推送的视图控制器上都有后退按钮。(只是不要添加[super setTitle:title]

- (void)setTitle:(NSString *)title
{
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    [lbl setText:title];
    [lbl setTextColor:[UIColor whiteColor]];
    [self.navigationItem setTitleView:lbl];
}

回答by smoothumut

for storyboard solution;

故事板解决方案;

click on the previous view controller's navigation item. then click on the attributes inspector on the right pane, then write " " or anything else on the backbutton area. this will tell view controller what to show when in the next(child) view controller. hope it helps

单击上一个视图控制器的导航项。然后单击右侧窗格上的属性检查器,然后在后退按钮区域写上“”或其他任何内容。这将告诉视图控制器在下一个(子)视图控制器中显示什么。希望能帮助到你

for code solution;

代码解决方案;

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:@"back off" style:UIBarButtonItemStyleBordered target:self action:@selector(popView)];
[[self navigationItem] setBackBarButtonItem:customBarItem];