按下后退按钮时,前一屏幕上的导航标题消失 [英] navigation title on previous screen disappear when back button pressed

查看:37
本文介绍了按下后退按钮时,前一屏幕上的导航标题消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导航栏标题有问题.我有 2 个屏幕,当我单击屏幕 2 上的后退按钮时,它将返回到屏幕 1,但屏幕 1 的标题消失了.这是我的代码片段

i have a problem with navigation bar title. i have 2 screen, when i click back button on screen 2 it will back to screen 1 but the title of screen 1 is disappear. Here is my snippet code

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 的标题出现,并使用以下技巧:

i've tried several tricks to make title of screen 1 appears with the following tricks:

  1. viewwillappear方法上设置标题

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

  • viewdidappear方法上设置标题

     - (void)viewDidAppear:(BOOL)animated{
     self.navigationController.navigationBar.topItem.title = @"YourTitle";
      self.title = @"tes";
      }
    

  • 但是标题仍然消失了.请帮忙

    but the title is still disappears. please help

    这是我移动到下一个屏幕的方式

    here is the way how i moving to next screen

    - (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];
    }
    

    推荐答案

    这解决了我的问题:

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

    这篇关于按下后退按钮时,前一屏幕上的导航标题消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆