Here is my code. I want to put a back button on the opening rootviewController.
- (void)selectSurah:(id)sender {
SurahTableViewController * surahTableViewController = [[SurahTableViewController alloc] initWithNibName:@"SurahTableViewController" bundle:nil];
surahTableViewController.navigationItem.title=@"Surah";
surahTableViewController.navigationItem.backBarButtonItem.title=@"Back";
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:surahTableViewController];
[self presentModalViewController:aNavigationController animated:YES];
}
I don't believe it's possible to pop the root view controller off the navigation stack, but you can fake it with a
UIButton
added as the custom view of aUIBarButtonItem
:A suitable PSD of iOS UI elements can be found here.
Faizan,
Helium3 comment makes sense.
I suppose that your button is needed to dismiss the controller presented modally, is it true? Correct if I'm wrong.
If so, you could just create a new
UIBarButtonItem
and set is a left (or right) button for theUINavigationController
navigationItem
. To not break encapsulation create it in theviewDidLoad
method for yourSurahTableViewController
controller.Since the
SurahTableViewController
is a root view controller in a navigation controller you can't go back to the root because you're already there. Since you've presented it modally from something else, you need to put a button on the nav bar that has anIBAction
which calls:Appearance and behavior of a back button in a UINavigationController relies on interaction between a stack of UINavigationControllers. Putting a back button on the first controller breaks this convention, there's nothing to go back to, which is why your code isn't working.
You'll need to manually add UIBarButtonItem to the title bar code like:
If you truly want it to look like a back button, you'll need to manually create the UIBarButtonItem with an image that mirrors the back button.
Another suggestion though, as it looks like you are attempting to use a back button to dismiss a modal view controller, I'd stick with something more conventional like a "Close" or "Done" button to close the modal view controller. A back button is really more appropriate for navigating a stack of UINavigationControllers.