0

I'm using the following code to try to make a "back" button for my app, the view that this code is located is in a modal view (if that has any bearing?):

navBar = [[UINavigationController alloc] initWithRootViewController:tvController]; [navBar.view setFrame:CGRectMake(0, 0, 320, 460)]; self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @"Back" style: self.navigationController.navigationItem.leftBarButtonItem.style target: self action: @selector(backAction)]; navBar.navigationItem.backBarButtonItem.enabled = YES; [self.view addSubview:navBar.view]; 

The view does not show at all, thank you for any tips!

EDIT: Even if I use a leftBarButtonItem, it still does not show up, I think there is some problem with the self.navigationItem bit of my code?

3

2 Answers 2

3

You need to make sure that when you present the modal view that you wrap it in a UINavigationController, then you'll have a valid navigation bar to manipulate. Otherwise you'll change the navigationItem all you want but it won't show up because you're not in a navigationController.

So when you go to present the view controller you're probably doing something like this.

SomeViewController *someViewController = [[[SomeViewController alloc] init] autorelease]; [self presentModalViewController:someViewController animated:YES]; 

What you want to do is present it like this

SomeViewController *someViewController = [[[SomeViewController alloc] init] autorelease]; UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:someViewController] autorelease] [self presentModalViewController:navigationController animated:YES]; 

Then when you're in the modal view you'll have a valid navigation bar that you can manipulate. Altering the leftBarButtonItem at that point will actually do something and be visible.

If you're trying to make this show a back button though you're probably "doing it wrong" typically if you're presenting something modally like this you'd show a "done" button. However by wrapping this with a navigation controller like this it does allow the modal view to then push and pop view controllers and operate as a normal navigation stack. But the root of it should probably have a "done" button not a back to return back to its previous state.

Sign up to request clarification or add additional context in comments.

9 Comments

Okay, that works well. However, I have this line of code: navBar = [[UINavigationController alloc] initWithRootViewController:tvController]; in the modal view and since navBar is no longer being used, I tried to do this: self.navigationController = [[UINavigationController alloc] initWithRootViewController:tvController]; but self.navigationController is a readonly property, any way around this?
any ideas? Completely lost at the moment
can you tell me more about what the tvController is? is this some sort of custom navigation bar that you're trying to use?
can you post the code where you're presenting the modal view.
so how do you want the tableView to fit into this?
|
1

The backBarButtonItem property needs to be defined on the previous item in your stack, i.e. on the view controller you are going back to, not the current one.

EDIT:

OK, I see now you are adding your own custom navigation bar. In that case, you cannot use the view controller's navigation item. You must instead push your own navigation items on to the navigation bar and access those instead. For example:

UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Back"]; item.leftBarButtonItem = ...; [navBar pushNavigationItem:item animated:NO]; 

2 Comments

Okay, so wouldn't I be able to use a leftBarButtonItem instead?
You can use leftBarButtonItem on the currently visible view controller, but you won't get the standard back button shape.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.