views:

666

answers:

1

I've added a navigation bar to a UIViewController. It is displayed from another UIViewController only. I'd like to have a left side back button that is shaped similar to an arrow, just like the normal navigation bar back button. It seems I can only add a bar button through IB. I'm guessing the back button needs to be added programmatically. Any suggestions on how I should do this?

Currently, in the RootController, I push another UIViewController (viewB) by simply doing an addSubView. In viewB, I want to display the navigation bar. The app is view based, not navigation controller based.

+3  A: 

If you're using a navigation controller:

MyViewController *_myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
[[self navigationController] pushViewController:_myViewController animated:YES];
UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = _backButton;
[_backButton release], _backButton = nil;
[_myViewController release], _myViewController = nil;

If you're not using a navigation controller, look into the Three20 style components to make custom bar buttons.

Alex Reynolds
That isn't doing anything.
4thSpace
You'll need to tweak it to your particular project's code. You might want to edit your question to include relevant code snippets, where you are in your first view controller and pushing your second view controller.
Alex Reynolds
I've updated the question. Thanks.
4thSpace
I've edited my answer. Take a look at the Three20 project.
Alex Reynolds
I'd rather stay away from 3rd party frameworks. All I need is to add a back button that has the shape of a regular back button. If that isn't possible, I'll just use a "done" button style or something similar.
4thSpace
Apple doesn't have a back-arrow button available except for navigation. So you'll either have to use your own custom button, a third-party option, or a different button style.
Alex Reynolds
Thanks. Since I did use addSubView, when I pop that view (removeFromSuperView), the previous UIViewControllers viewWillAppear doesn't fire. I'm guessing I'll need to use an event?
4thSpace
You're not popping or pushing unless you use a navigation controller. If you use addSubview etc. you have to manage your own view appearance.
Alex Reynolds