Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making back button for root view in UINavigationController

So I created a UINavigationController manually, set it as my UIWindow's rootViewController, and I would like to use a back button to exit the UINavigationController and load another viewController in its place. However, the backItem property of the UINavigationBar is readonly, so I don't know how to set it properly (it is readonly and defaults to nil in the root navigation view). How can I achieve this (or similar effect, I want to be able to effectively "exit" this UINavigationController by pressing the back button on the root view).

Alternatively, is this bad form? How should I escape the root view of a UINavigationController?

EDIT:

Attempted Legolas' solution with the following code: (some names have been changed)

UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:plvc]; // plvc being the first viewcontroller
MyAppDelegate* appDelegate = [Utility getAppDelegate];
appDelegate.window.rootViewController = navController;

UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Start" style:UIBarButtonItemStyleBordered target:self action:@selector(initializeStuff)];
navController.navigationItem.leftBarButtonItem = backButton;
[navController.navigationItem setHidesBackButton:YES animated:YES];
[navController.view setNeedsDisplay];

But the button does not display. What am I doing wrong? The other back buttons display properly, but this one still does not.

like image 775
adam_0 Avatar asked Jun 06 '11 02:06

adam_0


2 Answers

You can do this in a different approach.

Go to the method:

- (void)viewDidLoad

Hide the back button with

[self.navigationItem setHidesBackButton:YES animated:YES];

Create a new UIButton or a UIBarButtonItem, and place it in place of the back button.

You can then use an action when you click the button

- (IBAction) clickBackButton : (id) sender; //and push view controller to your required view.

Updating my answer: Use this in the viewDidLoad method //works like a charm //

[self.navigationItem setHidesBackButton:YES animated:YES];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Start" style:UIBarButtonItemStyleBordered target:self action:@selector(initializeStuff)];

self.navigationItem.leftBarButtonItem = backButton;
like image 170
Legolas Avatar answered Oct 19 '22 21:10

Legolas


There are a few approaches I can think of.

  1. Use your own "back button" icon of your own design, or a system default. Use leftBarButtonItem.
  2. Make an image of the default icon. Again, use leftBarButtonItem.
  3. Make a fake root view that you push into the UINavigationController stack. The sole purpose of this root view will be to launch the secondary view, or die when the user comes back to it. This way, you will never see UINavigationController without a back button. I've tested this (but not profiled it); performance impact seems negligible.

Also, check out <https://discussions.apple.com/message/8298537#8298537> from 2008. Same question.

The problem is, how does the user get out of a UINavigationController and back to the app? The root navigation bar doesn't have a back button or any sort of hook to exit.

To which someone replied:

To do what you want to do, the trick is to put the "super-root" controller inside the navigation controller, but have it set the nav controller's navigationBarHidden property to YES in viewWillAppear and NO in viewWillDisappear. (For bonus points, animate it when appropriate.)

like image 3
kanzure Avatar answered Oct 19 '22 20:10

kanzure