I came across a very subtle issue.
Usually things are okay, but occasionally the current UIviewController has no title. When I call another viewcontroller called via
[[fruitDB navigationController] pushViewController:fruitc animated:YES];
there is no "back" button. The area on the top left of the navigation bar is still active though and I can go back.
How can I make sure the back button is still active, even if there is no title?
you can set the backBarButtonItem of the navigation item of the view controller.
Specifically, somewhere in viewController1 before pushing viewController2, do the following...
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
When you push viewController2, the back button shown will be the backBarButtonItem of viewController1.
Note: Technically, apple recommends overriding the navigationItem method in your view controller, and adding buttons there, but it's really not an issue in your case.
Just before you push the next view controller why don't you try:
self.title = #"Back";??
Related
I'm trying to change the text of the back button from within Interface Builder, but when I select the Navigation Bar and go into the Attributes Inspector and set the Back Button text to "Close" it still shows up with the title of the previous view.
How do I get this to work?
The back button will always show the previous UIViewController title or backBarButtonItem defined.
So if you have "view1" and move to "view2" you need to set the backButton in "view1" so it's displayed correctly while "view2" is presented.
(In other word "view1" is responsible to provide what should be displayed in a back button that point to it)
In this way if "view1" is follow by any views all those will have the correct back button.
Try this in -(void)viewDidload, as this method is fired after the nib has loaded:
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
A quick and dirty trick is to set the "title" to what name you want in the button in the willDisappear override in the view controller. Make sure to set the "title" back to the correct name on the willAppear override view controller. One drawback with this technique is that on slower units you can see it change. On iPhone 4 and later it's hardly noticeable.
Hope this helps.
I am having an issue: I have a tab-bar based app. In MainWindow.xib for the tab that shows the nav controller it links to RootViewController.xib.
in RootViewController.xib I have created a navigation controller and have added a table view.
in RootViewController in viewDidLoad i have [self.view addSubview:navController.view];.
The tableview and navigation controller work well for navigation. pressing a cell pushes the controller, and bar buttons work in the pushed controller.
But when I use self.navController.navigationItem.rightBarButtonItem = barButton; nothing shows up at all. also changing self.navController.navigationItem to self..navigationItem ect.. doesn't help. What do you think the problem could be? I appreciate every answer.
fyi the barbuttonitem is setup with:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"Help" style:UIBarButtonItemStylePlain target:self action:#selector(help)];
If u want to use both controller navigation and tab bar controller the u check the sample code of apple documentation. In which both function are use so i given link below.
UIcatalogue
If you init the navigation controller with...
navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
Then you can set button with...
rootViewController.navigationItem.rightBarButtonItem = yourButton;
How can set self.navigationItem.backBarButtonItem of my RootViewController, so that the back button is rectangular instead of having a back arrow? I want to do this because I'm using a custom backBarButtonItem with an image of a grid of four squares (like the nine-square-gird image that the Facebook iPhone app uses for its home button).
Currently, in -[RootViewController initWitNibName:bundle:], I do:
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"go-home.png"]
style:UIBarButtonItemStylePlain
target:nil action:NULL];
Note: This does not cause a memory leak as I'm using ARC.
But, this makes the button have a left arrow. Is there a simple fix to make the button rectangular on all sides?
I know I could set the leftBarButtonItem for all of the view controllers that can get pushed from the RootViewController, but there are like five different options, so that'd be a lot of repetition. I guess I could make a method, e.g., +[Utils homeBarButtonItem], that creates the button above and then call self.navigationItem.leftBarButtonItem = [Utils homeBarButtonItem]; in each of the five view controllers' -viewDidLoad methods, but I'm wondering if there's a simple fix I'm missing.
Sadly the only way, as you suggest, is to use a leftBarButtonItem and use a button builder utility class.
Set the action of your leftBarButtonItem to pop the view controller and you're done.
[self.navigationController popViewControllerAnimated:YES];
I have a reference to a "UIBarButtonItem", is there a way I can add a custom "Back Navigation Button" to that item when it is not part of a Navigation based view?
I can add a left button:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Custom Back"
style:UIBarButtonItemStylePlain target:self
action:#selector(backAction:)];
menuItem.backBarButtonItem = backButton; //This doesn't seem to work.
menuItem.popOverNavigationItem.leftBarButtonItem = backButton; //This shows a normal button
So how could I make the leftmost button look like a back navigation button?
UPDATE: This other question answered the root of my problem that was leading me to try and do this non-standard UI setup:
iPad: Merge concept of SplitViewController and NavigationController in RootView?
I think the correct way to set the back button is to set it for the view controller that you would be going back to. For example:
RootViewController > DetailViewController
If you want the back button to say "Custom Back" whilst you're on DetailViewController, you have to actually set RootViewController's back button to "Custom Back".
Hope that makes sense.
Having just struggled with this: I think the answer is “no.” You'll need a controller hierarchy.
I have three Views, Splash, Login and List.
From Splash I just wait and then Push Login View, with the Navigation Bar hidden.
In Login I Show the NavigationBar, hide the BackButton1 and add a new RightButton1, then I check if Settings.bundle "login" and "pass" are set. If so, I push List View. Otherwise I stay in the Login view waiting for the user to fill the form and press a button.
In List View I add a new RightButton2 and a new BackButton2.
The problem is that if Settings.bundle data is not null, and in Login View I quickly push List View the RightButton1 appears in List View, or no buttons appear at all, or only BackButton2 doesn't appear...
The most strange thing of all is that everything was OK and all of sudden it started to get messy.
Login View:
// Making the Navigation Bar Visible
[self.navigationController setNavigationBarHidden:NO animated:NO];
// Hiding the Back Button in THIS view (Login)
[self.navigationItem setHidesBackButton:YES];
// Inserting the Logout Button for the Next View (List)
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Logout"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
UIBarButtonItem* newAccountButton = [[UIBarButtonItem alloc]
initWithTitle:#"New Account"
style:UIBarButtonItemStylePlain
target:self
action:#selector(newAccountButtonPressed)];
self.navigationItem.rightBarButtonItem = newAccountButton;
[newAccountButton release];
List View
// Making the Navigation Bar Visible
[self.navigationController setNavigationBarHidden:NO animated:NO];
UIBarButtonItem* aboutButton = [[UIBarButtonItem alloc]
initWithTitle:#"About"
style:UIBarButtonItemStylePlain
target:self
action:#selector(aboutButtonPressed)];
self.navigationItem.rightBarButtonItem = aboutButton;
[aboutButton release];
Any ideas ?
UPDATE: I've tested more and it seems to me that when I quickly go from Login to List the NavigationBar doesn't have enough time to add the buttons and add the buttons and the bars in the wrong order. I've used a performSelector to make Login view wait until push List and if the Delay is like 2secs, everthing goes fine, but if it is like 0.1secs the problem appears again.
I'm usign viewDidLoad.. How can I make the Login View only push the List View after everything in it's NavigationBar is already ok? In other words, how can I make the push wait all the previous commands without a delay with fixed timing ?
One thing to consider is making your splash screen a modal view controller, since you're having to do a lot of extra work to make it not appear in the navigation hierarchy. Just use the presentModalViewController:animated: method of your navigation controller.
It may make sense to make the login view modal as well.
Better still, use default.png as your splash screen, and pop up a modal view controller for login. That is assuming your splash screen is static.