Back button not appearing on pushed UIViewController - objective-c

I have a UITableViewController. When I click on a cell I want to push a new view. This works fine, but the new view doesn't have a back button. Why is this?
TableViewCode:
if([[NSUserDefaults standardUserDefaults] boolForKey:#"isLoggedIn"])
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
ProfileViewController* profileViewController = [[ProfileViewController alloc] initWithNibName:#"ProfileViewController" bundle:nil];
profileViewController.message = [NSDictionary dictionaryWithObjectsAndKeys:cell.textLabel.text, #"user_login", #"default", #"message_source", nil];
switch(indexPath.row) {
case kUsernameRow:
[self.navigationController pushViewController:profileViewController animated:YES];
[profileViewController release];
break;
case kAboutRow:
break;
case kTOSRow:
break;
}
}

If your table view controller is created from nib, its default title is #"" (notice: not nil, but an empty string).
Back button has a bug where it doesn't display if title of previous controller on navigation stack is an empty string, so inside your table view controller, you need to set title to either nil or some string in code, or some string in Interface Builder (can't set it to nil there afaik).

From Apple documentation:
The bar button item on the left side of the navigation bar allows for navigation back to the previous view controller on the navigation stack. The navigation controller updates the left side of the navigation bar as follows:
If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the leftBarButtonItem property of the view controller’s navigation item.
If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller has a valid item in its backBarButtonItem property, the navigation bar displays that item.
If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the stack. (If there is only one view controller on the navigation stack, no back button is displayed.)

Check if your Navigation controller has navigation bar enabled. Click on Navigation bar under Navigation Controller in IB and check if 'hidden' is un-ticked. If it is ticked, the navigation bar will not be shown and so the back button would be invisible too.

Related

Modal view controller pushed from tab view controller always returning to first tab

I have a view controller inside a tab bar controller (the view controller is item index 2). When I push a modal (camera picker) from the view controller, when it dismisses it always returns to the first view controller in the tab bar controller (item index 0). Now I COULD set the tab upon completion of dismissal, but that creates an ugly "flash effect" where it shows the first view controller for about a milisecond before going back to the right one. It's quite ugly from a design standpoint. How can I fix it?
Here's what I have now that is ugly:
[picker dismissViewControllerAnimated:NO completion:^{
UINavigationController * nav=self.navigationController;
RootTabBarController * root=(RootTabBarController *)nav.parentViewController;
[root showProfilePage]; //this calls setSelectedIndex in the tab bar controller
}];
I accidentally set SetSelectedIndex=0 in the tab bar controller view did appear.

How to add extra tab bar button in tab bar controller without adding view controller on that button in iOS

How to add tab bar item in tab bar view controller without adding a view controller with that and tap on that button no view controller are open. Tap on that tap bar button a alert will show.
On view did load create a UITabBarItem programmatically with a special tag. In your tab controller set the delegate to your view controller. On the method
- (void)tabBar:(UITabBar *)tabBar
didSelectItem:(UITabBarItem *)item
Check to see if the item has your custom tag and if so present an alert. If it requires a view to be linked with the UITabBarItem simply just set a view of another UITabBarItem for your alert view bar item.

Change Visuals On Another Tab

I have a tab bar view in my Xcode app and it has two views. View 1 and View 2. When i press a button on View 1 i want a label on View 2 to say hi but i am having trouble doing this. Does anyone have any suggestions.
In the button method, you can get a reference to the tab bar controller with the view controller's tabBarController property. From that reference, you can get the view2 controller reference from the tab bar controller's viewControllers array. Once you have that reference, you need to change that view controller's tabBarItem.title property, and then finally, you need to update the tab bar controller's viewControllers array.
-(IBAction)ChangeTabTitle:(id)sender {
UIViewController *cont2 = [self.tabBarController.viewControllers objectAtIndex:1];
cont2.tabBarItem.title =#"Hi";
self.tabBarController.viewControllers = self.tabBarController.viewControllers;
}

iOS - Set UIToolBarItem on UINavigationController

I drag out a toolbaritem in storyboard and set it on my nav controller, but when I run my code it's not there, is there something I'm missing?
EDIT:
Tried setting it in code as well in my viewDidLoad method:
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Map" style:UIBarButtonItemStyleBordered target:self action:#selector(viewMap)];
self.navigationItem.rightBarButtonItem = rightBarButton;
Won't work either.
Here's how it's set up in my storyboard:
UPDATE:
Just found my problem. In my controller code when I update it's contents I change the right bar button item for a spinner and never set it back to what it had before.
UINavigationController already has a toolbar built in. It has a property toolBarHidden which is set to YES by default, which is why it is not normally seen. If you are using storyboard you can easily make the built-in bottom toolbar visible by checking the checkbox "Shows Toolbar" in the inspector when the Navigation Controller is selected.
See the UINavigationController documentation here for more details.
EDIT:
Ok, it sounds like what you are trying to do is add a right button to your view controller's UINavigationItem. To do this in storyboard, drag a "Bar Button Item" from the Objects Library onto the Navigation Item in your ViewController. You can then set the title/style/etc of the bar button item. If nothing still shows up when you run your app, make sure that your ViewController is connected properly with a segue to the navigation controller.
Also make sure you are adding the Bar Button Item to your view controller's Navigation Item, NOT to the View Controller itself. Here is how the setup should look in your storyboard:
To add an item to a navigation bar, you need to add a Bar Button Item to the Navigation Item contained in the view controller. Go to your storyboard, find the right VC, and find the navigation item (it's in the hierarchy shown in the navigation controller 'scene'). Just drag a Bar Button Item into that hierarchy underneath the nav item, or directly onto the navbar in the visual builder display.
The navigation controller only looks at your VC's nav item when that VC is pushed onto the stack; hence modifying the VC's nav item in viewDidLoad has no effect.
(I've done this programmatically before but I don't have the code with me, so maybe I'll add that later...)

back button text does not change

For one of my view controller (extends UITableViewController), I need to configure its back button text to "Back". But the back button still shows up with parent view controller's title (the default).
- (void)viewDidLoad
{
...
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
}
You need to set the back button on the view controller that pushed the class onto the stack e.g. the controller that created and pushed the class you are showing
The docs for View Controller Programming Guide for iOS suggest that the UINavigationItems are kept in the Navigation Item Stack which provides the title and buttons for the current item on the stack.
Importantly (I added bits in [])
Although most of the navigation bar’s content is obtained from the topmost navigation item [in the navigation stack], a pointer to the back item [of the previous item in the stack] is maintained so that a back button (with the title of the preceding item) can be created.
Looking at the docs under the Configuring the Navigation Item Object section there is a diagram which shows the stack and the backItem pointing to the item below the top item in the stack. In your case the top item in the stack would refer to the UINavigationItem for the class you are showing and the backItem will be a pointer to the class that pushed it.
NB
Look at that section in the docs an image is worth a thousand words
Try this:
UIButton* backButton = [UIButton buttonWithType:101];
[backButton addTarget:self action:#selector(popView:) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backItem;
self.navigationItem.backBarButtonItem.enabled = NO;
self.navigationItem.leftBarButtonItem.enabled = YES;
[backButton release];
You need implement a simple method popView, something like this:
-(IBAction)popView:(id)sender
{
[self.navigationController popViewControllerAnimated:NO];
}
Here's a bit more information from the latest UINavigationController class reference, which tells you how to change the button in either the "pushing" or "pushed" controller (I'd add the link, but Apple keeps moving stuff around in subtle ways. Please use your favorite search engine).
In summary: if you are configuring the back button in the pushing (ie, the previous) controller, you access the backBarButtonItem. This perfect for configuring a consistently named back button regardless of the next controller's type. If you want/need to change it in the pushed (ie, the current) controller, you need to access the leftBarButtonItem.
The relevant text is here:
The bar button item on the left side of the navigation bar allows for
navigation back to the previous view controller on the navigation
stack. The navigation controller updates the left side of the
navigation bar as follows:
If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button
item, set the leftBarButtonItem property of the view controller’s
navigation item.
If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller
has a valid item in its backBarButtonItem property, the navigation bar
displays that item.
If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the
value of the title property of the previous view controller—that is,
the view controller one level down on the stack. (If there is only one
view controller on the navigation stack, no back button is displayed.)