Unwanted Edit button in my navigation bar? - iphone

I've got the following code in my app to display a modal view:
InfoTableViewController *infoTableViewController = [[[InfoTableViewController alloc] initWithNibName:nil bundle:nil] autorelease];
infoTableViewController.title = #"Pirateometer";
infoTableViewController.navigationItem.rightBarButtonItem =
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:#selector(dismissInfo)] autorelease];
navController = [[UINavigationController alloc] initWithRootViewController:infoTableViewController];
[self presentModalViewController:navController animated:YES];
[navController retain];
However when I run, instead of the Done button on the right of my navigation bar I have an Edit button. If I change .rightBarButton to .leftBarButton my Done button appears on the left as expected, but the Edit button is again there on the right.
Am I supposed to specially remove this unwanted Edit button in code, or am I doing something wrong that is making it appear in the first place? If I have to remove it, how do I go about doing so?

Make sure in your -viewDidLoad method of InfoTableViewController that you're not setting the right button to the edit button.
In the default UITableViewController subclass stub code, there is a commented out line which does this. Perhaps you've accidentally uncommented it?
Setting it in -viewDidLoad will execute after you've already set it in your included code here, as the method doesn't run until the viewController is actually loaded (ie when you present it modally).

Related

"Back" button doesn't show up in UINavigationController

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?
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.
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];

iPhone: Navigate to another view with transition style and Back button set

I have following code which add BackButton on my view's navigation item's tabbar. It works fine.
// Add back button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
[view.navigationController setNavigationBarHidden:NO animated:YES];
view.navigationItem.backBarButtonItem = backButton;
view.navigationController.navigationBar.barStyle = UIBarStyleBlack;
[backButton release];
I use following line to navigate to my new view controller but it gets presented with default animation.
[view.navigationController pushViewController:viewController2 animated:YES];
I want to change the way it gets presented so I tried to do it following way.
[viewController2 setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[view.navigationController presentModalViewController:viewController2 animated:YES];
This works fine but I lost my BackButton..!! Is there anyway to present my new view controller with specific transition style and also keep my back button as is?
Thanks.
I found the answer to your question. Put a navigation bar on your viewController2 and add a bar button to it using either interface builder or using code. Then in the action of button press
In your viewController2.m write this function and link it up with the button press if you have added the bar button through interface builder.
-(void) backButtonPressed : (id) sender
{
[self dismissModalViewControllerAnimated:YES];
}
This should solve your problem.
Doing a pushViewController will carry forward the navigationBar and all for you but in your case presenting a viewController as modalViewController will not give you the navigationBar by default so you have to add it manually in your modalViewController which is viewController2 in your case.
Please let me know if you are still facing problems. We can fix it soon and save you some time and frustration.
Cheers!!!

UIToolbar in UISplitView application

I am trying to show the UIToolBar in the RootView of a UISplitView application, the code is the following:
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(refresh:)];
self.toolbarItems = [NSArray arrayWithObjects:refreshItem, nil];
[refreshItem release];
However, what I see is:
There's black bar on top (I don't know where this came from, I don't need this) also the bar at the bottom, is there a way to resize it?
What I want is to get something like this:
Using something like this you can add a bar button item to the top of the controller:
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
self.navigationItem.leftBarButtonItem = refreshItem;
[refreshItem release];
You will make the button appear in the main view controller's title bar, as it's meant to be.
If you want to make the button appear in the bottom of the navigation controller you could try using this approach, instead:
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
[self setToolbarItems:[NSArray arrayWithObjects:refreshItem, nil animated:YES]];
[self.navigationController setToolbarHidden:NO]; //optional, don't remember if it's required ...
[refreshItem release];
For this piece of code to work correctly the side controller has to be a UINavigationController, otherwise you wouldn't be able to create and handle the toolbar. I tried this approach in a clean project and the toolbar renders perfectly.
I had the same issue and Just fixed it, Due to moving the code out of the Viewdid Load to lower down the Page,
As I had previous put in
- (UIBarButtonItem *)barButtonItem {
Moving the Code You used to under that, Worked and fixed the issue
Stewart
Just a note for anybody else who stumbles upon this question. I was having the same issue as adit. The problem turned out to be I was setting up and unhiding the toolbar in the viewDidLoad method instead of the viewWillAppear method. Those gaps are caused by setting up the toolbar before the view knows it's being displayed in landscape mode.
The safest and easiest solution is to setup the UINavigationController to display the toolbar and navigation bar in Interface Builder.
If it looks as expected in IB, it is very unlikely it will change at run-time.
If the toolbar is to be shown/hidden when navigating you should add the cod to do so in viewWillAppear: and allways call the super implementation, or unexpected things may occurs. Something like this tends to give the best results in a consistent manner:
-(void)viewWillAppear:(BOOL)animated;
{
[super viewWillApplear:animated];
[self.navigationController setToolbarHidden:NO
animated:animated];
}
Also make sure to show/hide the toolbar as need in viewWillAppear: for all view controllers in your navigation stack for best result.

why do toolbar items not appear when I automatically jump to that view based on stored state?

I'm trying to understand why when I automatically jump to a 2nd view (using UINavigationController) after startup and reviewing stored state, that the toolbar items do not appear?
When I go back to main page (via UINavigationController standard arrangements), and then then select the row in the UITableView, and go back into the same view again the toolbar items appear fine.
Code extracts to give the rough idea is:
mainController - normal selection based entry
via "didSelectRowAtIndexPath"
create new view controller and pushing (pushViewController) onto stack
mainController - upon restart & checking if previous state user was in 2nd layer view
In bottom of viewDidLoad method check the state for previous view
If need to then automatically jump to 2nd layer view by following same method as per the normal selection approach above - in fact I refactored the code for both to use the same method/code for this
2nd Layer View
within ViewDidLoad setup the toolbar - code for this in this method
Code:
- (void)setupToolbar {
[self.navigationController setToolbarHidden:NO];
UIBarButtonItem *increaseFontButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"icon_zoom_in.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(pressButtonIncreaseFont:)
];
UIBarButtonItem *decreaseFontButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"icon_zoom_out.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(pressButtonDecreaseFont:)
];
NSArray *items = [NSArray arrayWithObjects: increaseFontButton, decreaseFontButton, nil];
self.toolbarItems = items;
//release buttons
[increaseFontButton release];
[decreaseFontButton release];
}
Any ideas? Ideas for fault finding?
One feature of Objective-C I find highly annoying and error-prone is the silent failure of calling a method on a null object. After your first line in the setupToolBar method, check if the navigationController is null:
NSLog(#" navigationController is 0x%x", self.navigationController);
Is the navController created in the same manner for the restart case as in the regular case?
I worked out how to fix this through process of elimination, but I don't understand why :)
So what fixed it was changing the following line in the application delegate's "didFinishLaunchingWithOptions" method:
// OLD Entry - Did not work
//[self.window addSubview:navigationController.view];
// NEW Entry - Fixed it
self.window.rootViewController = self.navigationController;
Any ideas why?

Back button of Navigation Controller does not work!

For some reason, if I try to go back to the main menu using the back button on the upper left corner, only the title returns to the previous menu, but not the view controller. View controller would return to the previous menu only if I explicitly call popViewControllerAnimated using some other button.
Is there anyway to solve this? I think I've coded something wrong. Tried googling but couldn't find any cases like mine.
I'm getting the exact same problem. Here is my code:
- (IBAction) showGameView:(id) sender {
gameView = [[TCGameViewController alloc] initWithNibName:#"TCGameViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:gameView animated:YES];
[gameView release];
}
And when I am done with gameView, I do this:
[self.navigationController setNavigationBarHidden:NO animated:YES];
But all it does when I push the 'back' button is cycle through the navigation bar, but never pops the view. I don't even know how to debug it.
In my other view, "infoView" I call the same code as before except the NavBar is never hidden, but it works just fine.
helps!
This problem can occur when you override the following method in your custom view controller:
- (UINavigationItem*)navigationItem
But you don't specify a UIBarButtonItem for the leftBarButtonItem property of the returned UINavigationItem.
If you use a custom navigationItem, and want the standard back button functionality, you could add a method as follows (remember that every UIViewController has a reference to the navigationController that containts it):
- (void)backButtonTapped
{
[self.navigationController popViewControllerAnimated:YES];
}
And then setup part of the custom navigationItem as follows:
- (UINavigationItem*)navigationItem
{
UIBarButtonItem* newLeftBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backButtonTapped)];
UINavigationItem* navigationItem = [[[UINavigationItem alloc] init] autorelease];
Hope this helps.