Search code examples
iosobjective-ciphoneuibarbuttonitemuinavigationitem

self.navigationItem.backBarButtonItem cannot be set


I have a TableViewController named TVC and a cell in it. When I clicked on cell, TableViewController will push a new ViewController (VC) into its navigation controller. In this case, I set the [self.navigationItem.backBarButtonItem setTitle:@" "] in the viewDidLoad function of the TVC. But in the new pushed ViewController VC, the back button still has the "back" text. Can anyone explain this?

BTW, the TableViewController TVC is also pushed by another view controller VC2. I set the same function [self.navigationItem.backBarButtonItem setTitle:@" "] in the viewDidLoad function of the VC2 and the back button text displayed in the TVC is correctly hidden.


Solution

  • What Bimala and Ted pointed are two ways of approaching the issue. The former is to set a custom BackButtonItem and the latter is to simply use the default BackButton. But setting the title of the screen to "" may not always be acceptable. So u can choose to do the same in ViewWillDisappear method.

    OBJ C:

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

    SWIFT:

    override func viewWillDisappear(animated: Bool) {
        self.title = ""
        super.viewWillDisappear(true)
    }
    

    Also you can set the title back in ViewWillappear

    OBJ C:

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

    SWIFT:

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
        self.title = "your title"
    }