Herahadi
Herahadi

Reputation: 133

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

Upvotes: 7

Views: 9490

Answers (7)

Muhammad Iqbal
Muhammad Iqbal

Reputation: 119

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

Upvotes: 0

ozo
ozo

Reputation: 761

This solved my problem:

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

Upvotes: 15

raed
raed

Reputation: 5075

Objective c solution:

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

Upvotes: -1

Andrey M.
Andrey M.

Reputation: 3079

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

Upvotes: -1

andersryanc
andersryanc

Reputation: 979

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.

Upvotes: 2

singh.jitendra
singh.jitendra

Reputation: 500

In viewWillAppear implement this:

self.navigationItem.title="Title text";

Upvotes: 1

Hossein Mayboudi
Hossein Mayboudi

Reputation: 505

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 {
        //...
    }
}

Upvotes: -2

Related Questions