navigation title on previous screen disappear when back button pressed

Herahadi picture Herahadi · Jul 1, 2014 · Viewed 7.9k times · Source

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";
....

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

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

  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";
      }
    

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

Answer

ozo picture ozo · Sep 13, 2014

This solved my problem:

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