Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Back button not appearing on pushed UIViewController

I have a UITableViewController. When I click on a cell I want to push a new view. This works fine, but the new view doesn't have a back button. Why is this?

TableViewCode:

if([[NSUserDefaults standardUserDefaults] boolForKey:@"isLoggedIn"])
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    ProfileViewController* profileViewController = [[ProfileViewController alloc] initWithNibName:@"ProfileViewController" bundle:nil];
    profileViewController.message = [NSDictionary dictionaryWithObjectsAndKeys:cell.textLabel.text, @"user_login", @"default", @"message_source", nil];
    switch(indexPath.row) {
        case kUsernameRow:                  
            [self.navigationController pushViewController:profileViewController animated:YES];
            [profileViewController release];
            break;
        case kAboutRow:                 
            break;
        case kTOSRow:                   
            break;
    }
}
like image 990
Sheehan Alam Avatar asked Aug 30 '11 18:08

Sheehan Alam


2 Answers

If your table view controller is created from nib, its default title is @"" (notice: not nil, but an empty string).

Back button has a bug where it doesn't display if title of previous controller on navigation stack is an empty string, so inside your table view controller, you need to set title to either nil or some string in code, or some string in Interface Builder (can't set it to nil there afaik).

like image 152
Filip Radelic Avatar answered Oct 21 '22 14:10

Filip Radelic


From Apple documentation:

The bar button item on the left side of the navigation bar allows for navigation back to the previous view controller on the navigation stack. The navigation controller updates the left side of the navigation bar as follows:

If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the leftBarButtonItem property of the view controller’s navigation item.

If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller has a valid item in its backBarButtonItem property, the navigation bar displays that item.

If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the stack. (If there is only one view controller on the navigation stack, no back button is displayed.)

like image 44
Nekto Avatar answered Oct 21 '22 14:10

Nekto