当返回按键被按下时,上一个屏幕中的导航标题消失了。

7
我有一个导航栏标题的问题。我有两个屏幕,当我在第二个屏幕点击返回按钮时,它会返回到第一个屏幕,但是第一个屏幕的标题消失了。这是我的代码片段:
screen1.m
    - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.title = @"title 1";
....

屏幕2 .m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.title = self.stringTitle;

    // change the back button and add an event handler
    self.navigationItem.hidesBackButton = YES;
    //set custom image to button if needed
    UIImage *backButtonImage = [UIImage imageNamed:@"btn_back.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:backButtonImage forState:UIControlStateNormal];
    button.frame = CGRectMake(-12, 2, backButtonImage.size.width, backButtonImage.size.height);
    [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];

    UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, backButtonImage.size.width-15, backButtonImage.size.height)];
    [backButtonView addSubview:button];

    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
    self.navigationItem.leftBarButtonItem = customBarItem;

    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
....
}

- (void) backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

我已经尝试了几个技巧来让屏幕1的标题出现,具体如下:
  1. set title on viewwillappear method

    - (void)viewWillAppear:(BOOL)animated{self.title = @"tes";}
    
  2. set title on viewdidappear method

     - (void)viewDidAppear:(BOOL)animated{
     self.navigationController.navigationBar.topItem.title = @"YourTitle";
      self.title = @"tes";
      }
    
但是标题仍然消失了,请帮忙。
这里是我如何移动到下一个屏幕的方式。
- (IBAction)btGenerateTokenAction:(id)sender {
    SofttokenResultController *controller = [[SofttokenResultController alloc] initWithNibName:@"SofttokenResultController" bundle:nil];

    controller.navigationController.navigationBar.backItem.title = @"Kembali";
    [ViewLoadingUtil animateToNextView:controller from:self :@""];
}

+ (void) animateToNextView:(UIViewController *)controller from:(UIViewController *)fromController :(NSString *)navTitle{

    NSLog(@"animateToNextView called");

    [fromController.navigationController pushViewController:controller animated:YES];
}

2
尝试使用self.navigationItem.title - ChintaN -Maddy- Ramani
1
你在导航控制器标题视图中添加了任何视图吗? - Retro
@Suhail 是的,在移动到屏幕2之前它是可见的。 - Herahadi
@ChinttuRoxeNRamani 我尝试在viewDidLoad方法中添加self.navigationItem.title,但仍然消失了。 - Herahadi
@Fogmeister 我正在使用这种方法切换到下一个屏幕。编辑:在第一个线程中添加我如何切换到下一个屏幕的方法。 - Herahadi
显示剩余4条评论
7个回答

15

这解决了我的问题:

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated: animated)
        self.navigationItem.title="Title text"
}

2
从文档中得知... 如果你重写了这个方法,你必须在你的实现中调用super。 - Fogmeister
1
这并没有解决我的问题。我不得不添加Dispatch Queue。所以如果有人需要添加以下代码: DispatchQueue.main.async { self.tabBarController?.navigationItem.title = "你的屏幕标题" } - Nathan Barreto

2

在我的情况下,问题出在backItem?.title上。在我的视图控制器和UINavigationController的自定义子类中,我都将其设置为空字符串:

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    ...
    navigationController.navigationBar.backItem?.title = ""
}

我的猜测是,在使用popViewController(animated:)时,标题从backItem?.title获取并设置为主标题属性。

这虽然旧了点,但是这个 bug 仍然存在,因为我刚刚不得不面对它... - Raphael Ayres

1
viewWillAppear 中实现以下内容:
self.navigationItem.title="Title text";

0

这通常发生在您设置backItem?.title = ""时,因为返回控制器的标题取自此属性。我遇到了这个问题几个月,只是通过注释掉这行代码来解决它。


-1

Objective-C 解决方案:

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
    self.navigationItem.title = @"Title text";
}

从文档中得知... 如果你重写了这个方法,你必须在你的实现中调用super。 - Fogmeister
@Fogmeister 完成了 :) - raed
1
@raed 应该是 [super viewWillAppear: animated]; - wes

-1

适用于我的解决方案。你可以尝试这个:

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

    self.navigationController?.navigationBar.topItem?.title = ""
    self.navigationItem.title = "Title"
}

-2

我来自Swift世界。然而,我尝试了许多解决方案。这些问题都是由动画引起的。如果你将Bool更改为false,它将解决你的问题。最后,你可以将你的代码放在viewWillAppear函数中。

override func viewWillAppear(animated: Bool) {
    if animated {
        //...
    }
}

从文档中得知... 如果你重写了这个方法,你必须在你的实现中调用super。 - Fogmeister

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接