Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigation title on previous screen disappear when back button pressed

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];
}
like image 694
Herahadi Avatar asked Jul 01 '14 08:07

Herahadi


3 Answers

This solved my problem:

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated: animated)
        self.navigationItem.title="Title text"
}
like image 58
ozo Avatar answered Oct 20 '22 23:10

ozo


In my case, it was the backItem?.title that was the problem. In both my view controller and within a custom subclass of UINavigationController I was setting it to an empty string:

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

My guess would be that when using popViewController(animated:) the title is taken from backItem?.title and set into the main title attribute.

like image 40
andersryanc Avatar answered Oct 20 '22 22:10

andersryanc


In viewWillAppear implement this:

self.navigationItem.title="Title text";
like image 1
singh.jitendra Avatar answered Oct 20 '22 21:10

singh.jitendra