some_id
some_id

Reputation: 29896

Navigation controller doesnt add back button

I have a view which pushes a new view onto the screen.

When the new view is shown, there is no back button in the navigation controller.

I have the following in one view

enter image description here

and I push a new view using the following code

[photoViewController setAsset:[assets objectAtIndex:(cell.rowNumber * 4) + index]];
[[self navigationController] pushViewController:photoViewController animated:YES];
[photoViewController release];

When the view appears the following is visible

enter image description here

How do I add the back button and where would I add the same share button to the bar in the second view?

Thanks. :)

Upvotes: 2

Views: 6539

Answers (4)

Splendid
Splendid

Reputation: 1061

U should not set backButton on VIewDidLoad, instead u code it on DidSelect

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UIBarButtonItem *btn=[[UIBarButtonItem alloc]init];
btn.title=@"Back";
self.navigationItem.backBarButtonItem=btn;
[btn release];

  }

Upvotes: 0

Sabby
Sabby

Reputation: 2592

Try to use the NSZombieEnable in your enviornment variable in your projects executable ,this will help you to debug.Well this crash genrally occurs ,because array contains elements less then the count you are specifying.you need to work with your this code, i think so....

[photoViewController setAsset:[assets objectAtIndex:(cell.rowNumber * 4) + index]];

and also put the breakpoints in your table view method didselect row at index path,cell for row at index path and number of rows and hopefully you would be able to figure out

Upvotes: 0

Ken Joyner
Ken Joyner

Reputation: 921

You need to set a title in your first view. The back button uses the title of the previous view. If one is not set then the back button will not be displayed.

In the viewDidLoad method of your first view, use self.title = @"title";

Upvotes: 10

Matt Wilding
Matt Wilding

Reputation: 20153

It's odd that there is no back button displayed in the navigation bar for the pushed controller, because it is standard, out-of-the-box, behavior to give you one for free. Your photoViewController hasn't overridden -(UINavigationItem*)navigationItem;, has it?

As for adding the "Share" button to the second controller, you can just set it through the controller's navigationItem property when the its view loads:

// In the .m file of whatever class photoViewController is...  
- (void)viewDidLoad {

    UIBarButtonItem* rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Share" style:UIBarButtonItemStylePlain target:self action:@selector(doSomething:)];
    [self.navigationItem setRightBarButtonItem:rightBarButtonItem];
    [rightBarButtonItem release];
}

You can use the same approach to set a custom back button, just get yourself another UIBarButtonItem and set it as the leftBarButtonItem of your UINavigationItem.

Upvotes: 0

Related Questions