7

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];
}
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
Herahadi
  • 133
  • 2
  • 9

7 Answers7

15

This solved my problem:

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated: animated)
        self.navigationItem.title="Title text"
}
zzzkk
  • 49
  • 1
  • 10
ozo
  • 763
  • 7
  • 13
  • 2
    From the docs... `If you override this method, you must call super in your implementation.` – Fogmeister Oct 20 '16 at 10:44
  • 1
    This didnt solve my problem. I had to add the Dispatch Queue. So if anybody needs add the code DispatchQueue.main.async { self.tabBarController?.navigationItem.title = "Your screen title" } – Nathan Barreto Mar 21 '18 at 18:37
2

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.

andersryanc
  • 939
  • 7
  • 19
1

In viewWillAppear implement this:

self.navigationItem.title="Title text";
BytesGuy
  • 4,097
  • 6
  • 36
  • 55
singh.jitendra
  • 490
  • 1
  • 9
  • 19
0

it usually happen when you set backItem?.title = "" because the title of the back controller is taken from this property I face this issue from months and solved it just by commenting this line

Muhammad Iqbal
  • 109
  • 1
  • 9
-1

Objective c solution:

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
    self.navigationItem.title = @"Title text";
}
raed
  • 4,887
  • 4
  • 30
  • 49
-1

Working for me solution. You can try this one:

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

    self.navigationController?.navigationBar.topItem?.title = ""
    self.navigationItem.title = "Title"
}
Andrey M.
  • 3,021
  • 29
  • 42
-2

I come from the Swift world. However I tried many solutions. These were caused by the animation. If you change the Bool to false, it will solve your problem. Finally you can put your code in the viewWillAppear function.

override func viewWillAppear(animated: Bool) {
    if animated {
        //...
    }
}
Bradley Thomas
  • 4,060
  • 6
  • 33
  • 55
Hossein Mayboudi
  • 395
  • 1
  • 4
  • 12