227

目前左栏按钮默认值是加载当前视图的标题,换句话说,当按下按钮(后退按钮)时要显示的视图。

我想将按钮上显示的文本更改为其他内容。

我尝试将以下代码行放在视图控制器的 viewDidLoad 方法中,但它似乎不起作用。

self.navigationItem.leftBarButtonItem.title = @"Log Out";

我该怎么办?

谢谢。

4

37 回答 37

343

这应该放在调用标题为“NewTitle”的 ViewController 的方法中。就在 push 或 popViewController 语句之前。

UIBarButtonItem *newBackButton = 
        [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
[newBackButton release];
于 2009-09-19T19:58:37.203 回答
97

在 ChildVC 中,这对我有用......

self.navigationController.navigationBar.topItem.title = @"Back";

也可以在 Swift 中使用!

self.navigationController!.navigationBar.topItem!.title = "Back"
于 2013-02-19T14:09:37.187 回答
82

这是backBarButtonItem的文档:

“当此导航项位于堆栈中顶部项的正下方时,导航控制器会从此导航项派生导航栏的后退按钮。[...] 如果您想为后退按钮指定自定义图像或标题,您可以将自定义栏按钮项(带有您的自定义标题或图像)分配给此属性。"

视图控制器A“父”视图控制器):

self.title = @"Really Long Title";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Short" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;

当任何其他视图控制器B在导航堆栈的顶部,并且A在它的正下方时,B的后退按钮将具有标题"Short"

于 2012-06-14T10:43:45.237 回答
56

在使用情节提要的 Xcode 4.5 中,到目前为止,当后退按钮的值不必动态更改时,我发现的最简单的解决方案是使用与您的视图控制器的导航项关联的“后退按钮”字段想要“返回”按钮说点别的。

例如,在下面的屏幕截图中,我希望我推动的视图控制器的后退按钮具有“后退”作为后退按钮的标题。

在此处输入图像描述

当然,如果您需要后退按钮每次都说些稍有不同的内容,这将不起作用……这里有所有其他解决方案。

于 2012-12-10T01:46:31.190 回答
42

我知道,这个问题很老了,但我找到了一个很好的解决方案。

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"Custom Title";
self.navigationController.navigationBar.topItem.backBarButtonItem = barButton;

从 childView 工作!使用 iOS 7 测试。

于 2014-04-02T11:14:39.173 回答
26

也许我过于简单化了,但从 Apple 的文档来看,措辞是:

如果任何一个视图控制器都没有指定自定义条形按钮项,则使用默认的后退按钮并将其标题设置为前一个视图控制器的 title 属性的值——即视图控制器向下一层堆栈。

上面标记为正确的解决方案设置了父控制器的默认按钮项。这是正确的答案,但我通过self.title在将新控制器推送到 NavigationController 堆栈之前更改 UIViewController 的属性来解决问题。

这会自动更新下一个控制器上的后退按钮的标题,只要您设置self.title回它应该在的位置,viewWillAppear我就看不到这种方法会导致太多问题。

于 2009-10-21T03:04:33.837 回答
20

这对我来说更好。尝试 :

 self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] 
initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
于 2013-06-20T13:11:24.987 回答
20

在 Swift/iOS8 中,以下内容对我有用:

let backButton = UIBarButtonItem(
      title: "Back Button Text",
      style: UIBarButtonItemStyle.Bordered,
      target: nil,
      action: nil
);

self.navigationController.navigationBar.topItem.backBarButtonItem = backButton;

从菲利普的答案移植。

于 2014-08-22T00:04:09.083 回答
17

好的,方法在这里。如果您有一个“第一个”视图控制器并且您通过按下按钮等“第二个”导航另一个视图控制器,您需要做一些工作。首先,您需要像这样在“第二个”视图控制器的 ViewDidLoad 方法中创建一个 BarButtonItem;

    UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Back" 
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(OnClick_btnBack:)];
    self.navigationItem.leftBarButtonItem = btnBack;
    [btnBack release];

完成此操作后,您需要像这样在同一个 .m 文件中写入“btnBack”操作的代码;

-(IBAction)OnClick_btnBack:(id)sender  {
      [self.navigationController popViewControllerAnimated:YES];
    //[self.navigationController pushViewController:self.navigationController.parentViewController animated:YES];
}

就这样。

于 2009-09-19T23:01:37.400 回答
17

我有一个标题很长的父视图控制器。这导致后退按钮文本渗入子视图控制器的标题。

在尝试了一堆不同的解决方案之后,这就是我最终要做的(扩展 @john.k.doe 方法):

使用 Xcode 7.2、Swift 2

  1. 在 Storyboard 中,添加一个Navigation ItemParent View Controller 场景(不是子 VC)

导航项

  1. Attributes Inspector新的 中,在字段Navigation Item中输入一个space字符。Back Button稍后再谈。

视图层次结构中的导航项

向“后退按钮”字段添加空格字符

  1. 视图控制器中,添加以下代码:

片段:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    switch segue.destinationViewController {
    case is ChildViewController:
        navigationItem.backBarButtonItem?.title = ""
    default:
        navigationItem.backBarButtonItem?.title = "Full Parent Title"
    }
}

解释:

后退按钮属于父视图控制器。Navigation Item为您提供返回按钮的句柄,因此您可以在代码或情节提要中设置标题。

笔记:

如果您将Navigation Item Back Button文本保留为默认的空字符串,则返回按钮标题将变为“返回”。

其他方法有效,为什么要使用这个?:

虽然可以覆盖子视图控制器上的后退按钮标题,但要处理它是一个挑战,直到它已经在屏幕上短暂闪烁。

一些方法构建一个新的后退按钮并覆盖现有的。我确信它有效,并且在某些用例中可能是必要的。但我更喜欢尽可能利用现有的 API。

在某些情况下,更改title父视图控制器是最快的解决方案。但是,这会更改父标题,因此您必须管理状态。Tab Bar Controller由于标题更改会导致标题的副作用,因此事情也会变得混乱Tab Bar Item

于 2016-01-20T18:05:58.227 回答
10

对于那些使用故事板的人,只需选择父视图控制器框架(不是持有目标视图的那个)视图控制器框架(确保您在导航栏上单击右键,然后打开属性检查器,您将在其中找到三个表单输入。第三个“后退按钮”是我们正在寻找的。

于 2012-11-05T21:14:32.517 回答
8

斯威夫特版本:

在您的子视图控制器中:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.backItem?.title = "TEXT"
}
于 2015-11-27T15:19:31.683 回答
6

这是另一种方法。

在您的父视图控制器中,实现以下方法:

- (void) setBackBarButtonItemTitle:(NSString *)newTitle {
  self.navigationItem.backBarButtonItem.title = newTitle;
}

在您的子视图控制器中,当您想要更改标题时,这将起作用:

NSArray *viewControllerArray = [self.navigationController viewControllers];
int parentViewControllerIndex = [viewControllerArray count] - 2;
[[viewControllerArray objectAtIndex:parentViewControllerIndex] setBackBarButtonItemTitle:@"New Title"];

我永远无法让该parentViewController物业工作:

[(ParentViewController *)(self.navigationController.parentViewController) setBackBarButtonItemTitle:@"New Title"];

我不知道这是一个错误还是我没有正确使用它。但是抓取viewControllers数组中倒数第二个视图控制器指向父视图控制器,我可以使用该引用正确调用父方法。

于 2009-09-19T20:21:04.710 回答
6

行。我个人讨厌所有这些选择。因此我想出了我自己的。

根据我看到的信息。似乎上一个视图控制器控制着它自己的“返回”按钮,该按钮将显示在推送的视图控制器上。

我已经为想要更改后退按钮的控制器上的 navigationItem 创建了一个延迟加载方法。

我的是一个邀请买家控制器

Invite Buyer 是默认设置的文本。

但后退按钮需要邀请

这是我用来创建后退按钮的代码。

我将此代码放在控制器的实现 (.m) 文件的顶部,它会自动覆盖超级的方法。

- (UINavigationItem *)navigationItem{
    UINavigationItem *item = [super navigationItem];
    if (item != nil && item.backBarButtonItem == nil)
    {
        item.backBarButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
        item.backBarButtonItem.title = @"Invite";
    }

    return item;
}

我觉得这是一种更优雅的方式来实现这一点。

我将这段代码放在一个地方,它会在需要时自动填充。

无需在每次推送请求之前调用代码。

希望这可以帮助

于 2012-02-14T02:51:05.187 回答
6

对于斯威夫特:

    // Rename back button
    let backButton = UIBarButtonItem(
        title: "Back",
        style: UIBarButtonItemStyle.Plain, // Note: .Bordered is deprecated
        target: nil,
        action: nil
    )
    self.navigationController!.navigationBar.topItem!.backBarButtonItem = backButton
于 2015-11-19T08:40:54.477 回答
5
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Back" 
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(OnClick_btnBack:)];
    self.navigationItem.leftBarButtonItem = btnBack;
    [btnBack release];
于 2012-09-05T12:36:20.590 回答
5

这是答案:

viewDidAppear:animated(不在viewDidLoad)中执行以下操作

- (void)viewDidAppear:(BOOL)animated
{
     [self.navigationController.navigationBar.backItem setTitle:@"anything"];

     // then call the super
     [super viewDidAppear:animated];
}

如果你想保持后退按钮的形状。

于 2013-04-22T11:04:03.033 回答
5

这里解释的解决方案都不适合我。所以我所做的就是通过以下方式从我来自的场景中删除标题:

self.title = @"";

因此,当呈现新场景时,不会出现后面的文本。

我绝对同意这根本不是一个明确的解决方案,但是有效,并且没有任何解释对我有用。

于 2015-06-17T08:38:42.500 回答
5

我发现最好将导航堆栈中当前视图控制器的标题更改为所需的后退按钮文本,然后再推送到下一个视图控制器。

例如

self.navigationItem.title = @"Desired back button text";
[self.navigationController pushViewController:QAVC animated:NO];

然后在 viewDidAppear 中将标题设置回原始 VC 所需的标题。瞧!

于 2015-08-11T20:24:38.463 回答
4

self.navigationController.navigationBar.backItem.title = @"TEXT";

在 Swift 中:

self.navigationController?.navigationBar.backItem?.title = "TEXT"
于 2011-09-06T08:49:47.417 回答
4

我发现,更改后退按钮名称的最简单方法是将视图控制器标题设置为后退按钮的标题,然后将视图控制器导航项中的 titleView 替换为真实的自定义标签姓名。

像这样:

自定义视图控制器.m

@implementation CustomViewController

- (NSString*)title {
    return @"Back Button Title";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
    customTitleView.text = @"Navigation Bar Title";
    customTitleView.font = [UIFont boldSystemFontOfSize:20];
    customTitleView.backgroundColor = [UIColor clearColor];
    customTitleView.textColor = [UIColor whiteColor];
    customTitleView.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    customTitleView.shadowOffset = CGSizeMake(0, -1);

    [customTitleView sizeToFit];

    self.navigationItem.titleView = [customTitleView autorelease];
}

@end

这将使 UINavigationBar 中的标题看起来像是原生的。使视图控制器能够具有单独的标题和后退按钮标题。

在视图控制器 A 和 B 的情况下,A 负责告诉它的后退按钮应该如何显示,而 B 显示。

编辑:这也保持了后退按钮的原生外观(左箭头栏按钮项)。

于 2011-11-21T09:53:23.640 回答
4

这段代码也有效。把它放在导航控制器的根控制器上:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
于 2015-03-15T15:14:15.127 回答
4

我是 iOS 新手,但我将提供非常简单的覆盖导航控制器类的答案。我简单地覆盖了 push 和 pop 方法并保存了前一个视图控制器的标题。抱歉粘贴到 js 块中。有点困惑如何在普通代码块中过去。

#import "MyCustomNavController.h"


@implementation MyCustomNavController {

    NSString *_savedTitle;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated withBackBtnTitle:(NSString *)title {
    _savedTitle = self.topViewController.title;

    self.topViewController.title = title;
    [super pushViewController:viewController animated:animated];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {

    [self.viewControllers objectAtIndex:self.viewControllers.count - 2].title = _savedTitle;
    return [super popViewControllerAnimated:animated];
}

@end

于 2016-10-13T11:45:04.120 回答
3
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] 
                                              initWithTitle:@"Log out" 
                                              style:UIBarButtonItemStyleDone 
                                              target:nil 
                                              action:nil] autorelease];

您可以将它放在父控制器的代码中任何您喜欢的位置,这样您就可以为不同的子视图使用不同的后退按钮。

于 2011-08-31T11:04:54.310 回答
3

大多数解决方案会在添加具有所需标题的常用按钮时杀死 BackButton 的原始样式(左箭头栏按钮)。
所以要保持原来的风格有两种方法:
第一种:使用我不喜欢做的无证按钮风格(110或类似的东西)。但是,如果您愿意,可以在 stackoverflow 上找到如何操作。
第二:使用我Trenskow的想法。我喜欢它,我使用它有点改变。
而不是覆盖 - (NSString*)title 我决定以以下方式保留原始标题(这允许我使用 nib 的标题以及在推送状态下给定的标题 btw)。

- (void)viewDidLoad {
    [super viewDidLoad];
    static NSString * backButtonTitle=@"Back"; //or whatever u want

    if (![self.title isEqualToString:backButtonTitle]){

        UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
        customTitleView.text = self.title; // original title
        customTitleView.font = [UIFont boldSystemFontOfSize:20];
        customTitleView.backgroundColor = [UIColor clearColor];
        customTitleView.textColor = [UIColor whiteColor];
        customTitleView.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
        customTitleView.shadowOffset = CGSizeMake(0, -1);

        [customTitleView sizeToFit];

        self.navigationItem.titleView = [customTitleView autorelease];
        self.title = backButtonTitle; 
    }
}

这个解决方案效果很好,看起来很原生。此外,如果在 viewDidLoad 方法中使用它,它会阻止执行超过 1 次。
我也尝试过 Jessedc 的解决方案,但看起来很糟糕。它会导致用户标题栏从原始更改为 BackButton 的所需并返回。

于 2012-04-02T14:29:25.537 回答
2

这对我来说是以前发布的答案的“简化”版本。

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];

backButton.title = @"Go Back";

self.navigationItem.backBarButtonItem = backButton;

请记住将代码放在父视图控制器(例如具有您的表视图或 UITableViewController 的视图)中,而不是子视图或详细视图(例如 UIViewController)。

您可以像这样轻松地本地化后退按钮字符串:

backButton.title = NSLocalizedString(@"Back Title", nil);
于 2014-02-23T20:17:50.177 回答
2

根据> _ _UINavigationBarbackItem

在此处输入图像描述

如果最顶部导航项的 leftBarButtonItem 属性为 nil,则导航栏显示一个后退按钮,其标题派生自该属性中的项。

但设置backItem.backBarButtonItem在第一次 viewWillAppear 中不起作用。topItem.backBarButtonItem在第一次 viewWillAppear 中设置唯一的作品。因为navigationBar.topItem仍然指向previousViewController.navigationItem. 在viewWillLayoutSubviews中,topItembackItem被更新。所以在第一次之后viewWillAppear,我们应该设置backItem.backBarButtonItem.

解答:backBarButtonItemnavigationItem无论何时何地在您当前的 viewController(顶部 viewController)中,都将 a 设置前一个 viewController的。viewWillAppear您可以在或中使用此代码viewDidLoad。查看我的博文iOS Set Navigation Bar Back Button Title进行详细分析。

 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];
    }
于 2014-08-22T09:44:43.257 回答
2

我们有两个 VC 的 A 和 B。

如果要在 B 中更改标题,请在 A 中编写此代码

- (IBAction)goToBViewController:(UIButton *)sender {

    BViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"VC"];
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Your title here"
                                                                  style:UIBarButtonItemStylePlain
                                                                 target:nil
                                                                 action:nil];
    [[self navigationItem] setBackBarButtonItem:newBackButton];
    [self.navigationController pushViewController:vc animated:NO];

}

斯威夫特 4.1 Xcode 9.4

let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "VC"])
let newBackButton = UIBarButtonItem.init(title: "Your title here", style: UIBarButtonItemStyle.plain, target: nil, action: nil)
navigationController?.navigationBar.topItem?.backBarButtonItem = newBackButton
navigationController?.pushViewController(secondViewController!, animated: true)
于 2018-02-16T08:31:53.267 回答
1

斯坦的回答是最好的。但它也有一个问题,当您使用带有标签栏的控制器并更改控制器的标题时,您也可以更改标签栏的标题。所以最好的答案是只更改 view_controller.navigationItem.title 并使用 view_controller.navigationItem .title 在函数中。答案在这里:(使用 ARC 并将它们添加到视图的 viewDidLoad 中)

  static NSString * back_button_title=@"Back"; //or whatever u want
  if (![view_controller.navigationItem.title isEqualToString:back_button_title]){
    UILabel* custom_title_view = [[UILabel alloc] initWithFrame:CGRectZero];
    custom_title_view.text = view_controller.navigationItem.title; // original title
    custom_title_view.font = [UIFont boldSystemFontOfSize:20];
    custom_title_view.backgroundColor = [UIColor clearColor];
    custom_title_view.textColor = [UIColor whiteColor];
    custom_title_view.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    custom_title_view.shadowOffset = CGSizeMake(0, -1);

    [custom_title_view sizeToFit];

    view_controller.navigationItem.titleView = custom_title_view;
    view_controller.navigationItem.title = back_button_title;
  }

在我自己的使用中,我把它做成了这样的一个函数,只要在viewDidLoad中有一行代码就可以了。

+ (void)makeSubViewHaveBackButton:(UIViewController*) view_controller{
  static NSString * back_button_title=@"Back"; //or whatever u want
  if (![view_controller.navigationItem.title isEqualToString:back_button_title]){
    UILabel* custom_title_view = [[UILabel alloc] initWithFrame:CGRectZero];
    custom_title_view.text = view_controller.navigationItem.title; // original title
    custom_title_view.font = [UIFont boldSystemFontOfSize:20];
    custom_title_view.backgroundColor = [UIColor clearColor];
    custom_title_view.textColor = [UIColor whiteColor];
    custom_title_view.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    custom_title_view.shadowOffset = CGSizeMake(0, -1);

    [custom_title_view sizeToFit];

    view_controller.navigationItem.titleView = custom_title_view;
    view_controller.navigationItem.title = back_button_title;
  }
}
于 2012-11-30T15:27:39.373 回答
1

如果您不仅想将 Back 按钮的文本更改为相同的文本并保持原来的左箭头形状,而且还想在用户单击 Back 按钮时做一些事情,我建议您看看我的“ CustomNavigationController ” .

于 2013-12-31T05:02:33.487 回答
1

问题:导航栏中的“返回”文本无法替换。

原因:推送视图后导航栏中设置了“返回”标签,因为父视图控制器中的 .title 属性设置为 nil(或未初始化)。

一种解决方案:如果您设置 self.title="Whatever...",您将看到在推送新视图控制器后,将出现“Whatever...”而不是“Back”。

于 2015-04-03T18:08:18.253 回答
1

使用下面的代码行:

UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:@"hello"
                                 style:UIBarButtonItemStylePlain
                                target:nil
                                action:nil];

self.navigationItem.leftBarButtonItems =[[NSArray alloc] initWithObjects:newBackButton, nil];

self.navigationItem.leftItemsSupplementBackButton = YES;
于 2016-03-30T12:48:57.603 回答
1

Swift 4
iOS 11.2
Xcode 9.2

TableViewController1  ---segue--->   TableViewController2

您可以在 TableViewController1 或 TableViewController2 中更改后退按钮的文本。

更改 TableViewController1 中的后退按钮文本

1) 在viewWillAppear()

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let myBackButton = UIBarButtonItem()
    myBackButton.title = "Custom text"
    navigationItem.backBarButtonItem = myBackButton
}

出于某种原因,viewDidLoad() 为时过早将后退按钮添加到 NavigationItem。要连接两个 TableViewController,请在情节提要控件中将 TableViewController1 中的 TableViewCell 拖到 TableViewController2 的中间,然后在弹出菜单中选择Selection Segue > Show

2) 在tableView(_:didSelectRowAt:)

override func tableView(_ tableView: UITableView, didSelectRowAt: IndexPath) {

    let myButton = UIBarButtonItem()
    myButton.title = "Custom text"
    navigationItem.backBarButtonItem = myButton

    performSegue(withIdentifier: "ShowMyCustomBackButton", sender: nil)
}

要连接两个 TableViewController,在故事板控件中,将 TableViewController1 上方的黄色小圆圈拖到 TableViewController2 的中间,然后从弹出菜单中选择Manual Segue > Show。然后选择连接两个 TableViewController 的 segue,并在“Identifier”旁边的 Attributes Inspector 中输入“ShowMyCustomBackButton”。

3) 在storyboard

如果您只需要后退按钮的静态自定义文本,请为 TableViewController1 选择 NavigationItem(它<在情节提要的目录中有一个图标),然后打开 Attributes Inspector 并在“Back Button”字段中输入您的自定义文本(请务必从该字段中删除以使更改生效)。


更改 TableViewController2 中的后退按钮文本

1) 在viewWillAppear()

class MySecondTableViewController: UITableViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let myBackButton = UIBarButtonItem(
            title: "<Custom text",
            style: .plain,
            target: self,
            action: #selector(goBack)  //selector() needs to be paired with an @objc label on the method
        )

        navigationItem.leftBarButtonItem = myBackButton
    }

    @objc func goBack() {
        navigationController?.popViewController(animated: true)
    }

要连接两个 TableViewController,请在情节提要控件中将 TableViewController1 中的 TableViewCell 拖到 TableViewController2 的中间,然后在弹出菜单中选择Selection Segue > Show

于 2018-02-02T21:54:17.900 回答
1

如果您有多个导航

ParentViewController --> ChildViewController1 --> ChildViewController2

您可以使用以下代码更改导航栏上后退按钮的标题。

self.navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem.init(title: "Back", style: .plain, target: nil, action:nil)

于 2019-05-25T10:25:24.170 回答
0

导航控制器似乎在寻找

previousViewController.navigationItem.title

如果那里什么都没有,它会寻找

previousViewController.title
于 2014-04-05T20:17:55.360 回答
0

这在 Swift 4 和 iOS 11 中适用于我。最常见的情况是,当用户点击表格视图中的一行并将新的视图控制器推送到导航堆栈时,我需要这样做。通常我希望后退按钮是被点击的行所代表的对象的名称或某些属性。所以我通常这样做didSelectRowAt...

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let object = // get the object this row represents

    let backButton = UIBarButtonItem()
    backButton.title = object.name // or whatever
    self.navigationItem.backBarButtonItem = backButton

    // now show or segue to the next view controller
}
于 2017-10-28T15:37:38.140 回答
0

iOS 11+ 解决方案 - 不需要再次创建后退按钮。

在前一个屏幕上设置backButtonTitle空格。

// If navigation is from A -> B, set in A's `viewDidLoad`.
navigationItem.backButtonTitle = " "
于 2021-01-15T12:03:18.890 回答